Maven Assembly Plugin: Unpacking overwrites different versions of dependencies - maven

I am using Maven assembly plugin with which I generate single Jar file with all dependencies for my application (unpacked by definition in jarlib.xml given here: https://gist.github.com/knyttl/7cc0730ae0fb6947cbda). This dependency.jar can be then put on class path with my application.jar and run as java -cp application.jar:dependencies.jar my.class.Runner. The problem are however multiple versions of the same artifacts when unpacking jars.
For instance I am using org.apache.xmlrpc:xmlrpc-server:jar:3.1.3 which depends on javax.servlet:servlet-api:jar:2.3. In my application I need to use different, newer version of the javax.servlet, but when unpacking, the new version is skipped and the old one is used instead.
Is there a way to ignore the dependency given by xmlrpc-server?
Is there a way to prioritize the newer version of javax.servlet?
Is there a way to create single jar without unpacking the dependencies and being able to use them with -cp application.jar:dependencies.jar? When I tried to build the jar without unpacking, none of the inner jar classes were found when running the application.

Sounds like what you really want is the shade plugin - the ability to create a single jar with all of these included. https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html

The best solution I found is using <exclusions> directly in the <dependency> tag directly in the pom, without any plugins.

Related

How to sort dependencies in a section of a Maven POM file

I need to sort the dependencies in the dependencyManagement section of a POM file that is used as a parent for all projects of my team's portfolio.
The motivation for this is similar to the one described here. In my case, I am just trying to harmonize the versions used throughout our portfolio, in order to avoid the recurring nightmare of version discrepancy: due to copy-paste, some projects use a version of a dependency while others use another version. Another motivation is to have only one place where to manage dependencies. What I am doing is essentially merging dependency specifications from all modules into a giant dependencyManagement section of a parent POM. (EDIT: In the process of looking for an answer to this need of mine, I learned that such a POM is what Maven calls a BOM or "bill of materials".)
However, this task requires that I define the version of each dependency in our parent POM. Doing so, I find myself putting the dependency specification somewhere in what is a growing list of dependencies. It gets more and more difficult to add a dependency and find out whether the dependency is already specified. But that would be much easier if I could sort the dependencies, for instance by group ID.
Is there a plugin that serves that purpose of reordering the dependencies? As a last resort, I will end up writing a small program that will read the XML file from the parent POM and output it sorted.
The recently released version 0.2.0 of BOM Helper Maven Plugin now has the sort goal that does exactly that.
You need to add the plugin to your pom:
<plugin>
<groupId>com.commsen.maven</groupId>
<artifactId>bom-helper-maven-plugin</artifactId>
<version>0.2.0</version>
</plugin>
You can configure it to run on every build, but I would rather run it manually only when I add/change a dependency. Something like:
mvn bom-helper:sort -Dbom-helper.inplace=true
should do the job. See the docs for more options.
I used sortpom. See more about parameters at https://github.com/Ekryd/sortpom/wiki/Parameters
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort -Dsort.encoding=UTF-8 -Dsort.sortDependencies=scope,groupId,artifactId -Dsort.sortPlugins=groupId,artifactId -Dsort.sortProperties=true -Dsort.sortExecutions=true -Dsort.sortDependencyExclusions=groupId,artifactId -Dsort.lineSeparator="\n" -Dsort.ignoreLineSeparators="false" -Dsort.expandEmptyElements=false -Dsort.nrOfIndentSpace=2 -Dsort.indentSchemaLocation=true
The easiest way to sort your dependencies is to use the sortpom maven plugin.
It is very easy to use. Just go to your project directory, open terminal and run the following command:
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort -Dsort.keepBlankLines -Dsort.predefinedSortOrder=custom_1
To find more about the project, check their github repo: https://github.com/Ekryd/sortpom

Using JGitVer in a tycho pomless build

I would like to use the jgitver maven plugin to automatic define the version of eclipse plugins, features, repository that are built with tycho in pomless mode.
The version management of this 3 plugins are:
jgitver computes a version depending on git tree and tags, create a logical copy of pom, set the new version and associate the new pom to the current plugin in the maven reactor.
For tycho, version are defined in pom and in manifest.mf or feature.xml files. A version consistency check is performed.
When the pom is not defined, pomless maven generates a temporary pom file named .polyglot.build.properties using versions defined in manifest or feature file.
The input path are defined here and depends on PolyglotModelUtil.
The easier way would be a Jgitver evolution to locally update the feature.xml and manifest.MF. It would be a bad idea because a local file would be modified that is not compliant with the plugin philosophy.
A better way would be a Jgitver evolution to generate temporary feature.xml and manifest.MF files that would have been updated with the computed version. Then tycho and tycho-pomless should be forced to use them.
It seems to be possible if i succeed in setting a new ModelProcessor.LOCATION option
Do you think it's an effective way to solve this compatibility problem?
Do you see an alternative?
Thanks for your help.
You could also use directly the jgitver library to participate into your build environment ; it is a pure java library.
The library is the base of both the maven and gradle plugins.

Maven - load all jars inside a system folder

Is there a way we can load all the jar files inside a folder, as dependencies in a maven project.
So that, I do not have to mention each and every jar files in pom.xml, just mention or tell maven to pick all the jar files from folder 'x' and build the system.
Is this supported by maven?
I think this is supported by ant. Not sure whether gradle supports either.
In
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies
you see that you can reference single files, but there is no mechanism for directories. As I mentioned in the comment, using the disk is discouraged in general.
If you need the same set of dependencies in many projects, you can write a pom for that and use it (as parent or by setting a dependency to it).

What is the purpose of providing a downloaded pom.xml on mvnrepository.com

On mvnrepositry, when you search for a certain module, there's a link to download the binary. For some versions it has a pom.xml file available for download instead of the jar. What are you supposed to do with that pom.xml? It seems like if I specify a version that does not have a downloadable jar, but instead downloadable pom.xml, my maven build will fail. Is what I'm seeing correct?
Modules that only have pom files are maven modules with pom packaging. They are used to aggregate multiple modules into one unit. You can use such a module as a dependency for your maven project. Maven will download the pom file, analyze the dependencies included in that pom file and download those & add it to your automatically.
Even modules that have jars (jar packaging) have a pom file associated with them. This pom file defines the other dependencies that are required for using it. Maven will automatically process and fetch those dependencies (transitive dependencies).
This makes specifying and managing dependency for any project. You will specify the top level modules that your projects directly depends on and other things required will automatically figured out and downloaded. It also makes it easier when you have upgrade to a new version - all the transitive dependencies will get upgraded automatically.
One of the reason that cause this is because of licensing issue.
License for such JARs prohibit public redistribution in such approach. So someone provide only the POM so that you can get the JAR yourself and install it to your local maven repo/ internal repo, together with the POM provided.

Javadoc creation with maven

We have created a new artifact to generate javadoc. We have 40 artifacts defined as dependencies. Task is to create javadoc.jar and html pages for the 40 dependency artifacts.
Whats the best approach to achieve this in maven?
This is very unusual. Javadoc works on sources, not compiled classes, whereas maven dependencies reference classes, not sources.
So to make this work you'll have to do all of this:
since this is a dedicated javadoc artifact, it won't have a main JAR artifact, so you'll probably want to set the packaging to POM
make sure all your referenced artifacts have attached sources
add <classifier>sources</classifier> to all your dependencies
unpack all dependencies to a common root folder using dependency:unpack-dependencies
run javadoc on that folder
create a jar using the maven-assembly-plugin
attach that jar to the build using the buildhelper plugin
On re-reading the question: I'm assuming that you want to create combined docs of all dependencies. If not, you'll need 40 separate executions each of the javadoc, assembly and buildhelper plugins. Good luck with that.
A slightly more automated approach than the answer above:
So to make this work you'll have to do all of this:
since this is a dedicated javadoc artifact, it won't have a main JAR artifact, so you'll probably want to set the packaging to POM
make sure all your referenced artifacts have attached sources
add <classifier>sources</classifier> to all your dependencies
unpack all dependencies to a common root folder using dependency:unpack-dependencies
Change your sources directory to where you unpacked all the dependencies
Use the source plugin to manage all the Javadoc generation and deployment

Resources