I have a multi-module maven project: Module A depends on Module B. When Module B is built it is named ModuleB.jar and copied to target directory. Now in Module A's pom I have to put
<dependency>
<groupid>com.mycompany.app</groupid>
<artificatId>ModuleB</artifactId>
<version>1.0</version>
</dependency>
I have to put the version number, but then now I have two same Module B jars: ModuleB.jar (no version in name) and ModuleB-1.0.jar along with ModuleA.jar in the target directory.
I prefer to keep the version number out from the jar's final names since we have other legacy apps depending on these jars
Is there anyway to add Module B as a dependency in Module A's pom without the version number? Any clean solutions at all or am I just making life difficult for myself?
EDIT:
To clarify: These modules are built from a parent pom as jars and are copied, along with their dependencies, to a target directory outside of the project's parent directory. Hence the reason why there are two ModuleB jars: one when Module B is compiled and packaged with the finalname set as ModuleB.jar and one when Module A is compiled and packaged along with its dependencies, which includes Module B but with the name ModuleB-1.0.jar
Thanks!
The copy-dependency execution (I could strip out the version during the copy but then I would lose versions on 3rd party jars such as Spring jars =/ ):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${build.dir}/lib</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
So the problem was that I was copying my internal modules twice: once when running maven-jar-plugin (with no version in finalname) and once when running maven-dependency-plugin, and both plugins are outputting to the same target directory giving me ModuleB.jar and ModuleB-1.0.jar.
So in the parent pom I configured the plugins:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>${build.dir}/lib</outputDirectory>
</configuration>
</plugin>
and
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${build.dir}/lib</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
<stripVersion>false</stripVersion>
<excludeGroupIds>com.mycompany.app</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
The first plugin is building all my child modules and jar'ing them to the output directory specified. the second plugin is copying all my module's dependencies to the same output directory in the first plugin sans the internal modules using <excludeGroupIds>
Thank you for helping me realize there was a simple solution!
Related
To package a maven project with its dependencies, among many solutions, one may use maven-dependency-plugin with its goal copy-dependencies to get the dependencies in a folder besides, or one may use maven-shade-plugin to get all the code in a single jar.
I actually do both: I choose to have external dependencies (e.g. apache commons) as external libs, and my own dependencies (I have a multi-module maven parent project) shaded into a unique jar.
And it works, except for the classpath. I copy-dependencies with option excludeGroupIds to exclude my own maven group id. I shade with option to include only my own maven group id. Before that, I jar with option to add classpath to the manifest. All set, it works. But my classpath also contains my own dependencies that were actually shaded in the final jar.
It is no big deal, because the result works even with this erroneous classpath. But I wonder if there is a simple means to have the correct classpath, in order not to expose my internal structure to my users.
Here is a basic example demonstrating the problem:
<groupId>com.foo.bar</groupId>
<artifactId>com.foo.bar.launcher</artifactId>
<dependencies>
<dependency>
<groupId>com.foo.bar</groupId>
<artifactId>com.foo.bar.utils</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeGroupIds>com.foo.bar</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.foo.bar:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
The resulting manifest contains this:
Class-Path: lib/com.foo.bar.utils-0.0.1.jar lib/commons-lang3-3.8.1.jar while the com.foo.bar.utils one does not exist.
If you look into the following mvnrepository link maven shade plugin depends upon maven dependency tree. As per the above pom.xml maven dependency plugin, you have excluded com.foo.bar dependency. You can omit the maven-dependency-plugin to create fat jar. It is not mandatory to use in case of shade plugin.
You can use the following command to check and copy all the dependencies used in the project.
mvn dependency:copy-dependencies
https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin/3.2.1
I have a multi-module build that produces several executable JAR's. Each of the sub modules have a plugin defined in the POM, maven-dependency-plugin, that copy all necessary dependencies to a localized folder in the target directory. Here is what that looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I would like to move this into my parent POM and only activate the plugin when the sub module is an executable JAR. What would be the best approach to accomplish this? Ideally, if possible, it would be nice to define a <property> in each executable sub module that would inherit the plugin when the property is present.
So I understand how I can package dependencies into my executable JAR, using jar-with-dependencies descriptor for maven-assembly-plugin.
However, I want to also create a source bundle(s), that not only includes sources of my project, but sources of all dependencies that are embedded in my executable JAR.
How can one achieve that?
This is what I used finally:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>src-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
It copies the sources of all known dependencies recursively into the target/source directory. Pretty handy!
Note: Use unpack-dependencies goal to instead unpack all sources in destination directory.
Reference: https://maven.apache.org/plugins/maven-dependency-plugin/index.html
In my maven project I have plenty of dependencies which source code I need to get.
I know there is maven-dependency plugin with unpack-dependencies goal in it
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>resolve-dependecies</id>
<phase>process-sources</phase>
<goals>
<goal>sources</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<includeParents>true</includeParents>
<type>java-source</type>
</configuration>
</execution>
<execution>
<id>src-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.basedir}/Utils/src</outputDirectory>
<type>java-source</type>
</configuration>
</execution>
</executions>
</plugin>
But when I use it a lot files "have NOT been resolved".
e.g.
...
org.jboss:jboss-parent:java-source:sources:5
org.apache.cxf:cxf-parent:java-source:sources:3.0.3
org.apache:apache:java-source:sources:13
org.apache:apache:java-source:sources:9
org.liquibase:liquibase-parent:java-source:sources:3.5.0
org.apache.commons:commons-parent:java-source:sources:5
org.eclipse.jetty.orbit:jetty-orbit:java-source:sources:1
...
In pom.xml I didn't specify any repository for dependencies, so by default maven central repository is using.
I also tried to decompile (Fernflower, Procyon, JAD projects) my project jar file which contains all dependencies. But after decompiling hundreds of errors appeared in result java files.
I still hope to solve this issue using maven tools.
Thanks in advance for any help.
EDIT
Unresolved artifacts directly not specified in pom.xml
I see a couple of dependencies with *-parent. Given the name I guess these are merely parent poms which probably don't contain any java sources. For the remaining ones, are you sure that there are artifacts with the java sources?
UPDATE: I checked the list you shown within your question whether there are sources and there weren't. What I noted is that all of them have packaging POM. You might skip these by including <includeClassifiers>sources</includeClassifier> within your configuration
There is this maven module, say prj-package-module, to package the project artifacts into a tar file using maven-assembly-plugin. There are also jars added as dependencies in the prj-package-module/pom.xml and packaged into the tar file.
Now the requirement is to add a file, prj-pakacge-module/src/main/resources/file.xml to one of these dependency jars before packaging into the tar file. How can I achieve this?
Edit: The file is a JNLP with list of dependency jars dynamically added to it. For security reasons, Javaws also requires the JNLP file to be added a jar and the jar to be signed. This is where I hit the problem.
Here is a maven solution to dynamically unpack an existing dependency, add (copy) a resource to the unpacked folder, repack (jar) the hole and as such get a modified copy of the initial jar.
A sample pom file doing exactly that for the junit dependency:
<build>
<plugins>
<!-- unpack step -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<type>jar</type>
<outputDirectory>${project.build.directory}/unpack-tmp</outputDirectory>
<includes>**/*.class,**/*.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- add the additional resource step -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/unpack-tmp</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<include>test.properties</include>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- repack step -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>repack</id>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${basedir}/target/unpack-tmp</classesDirectory>
<finalName>junit-modified</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
What it is actually doing:
As part of the prepare-package phase we are adding three steps to create a modified jar from a dependency
First step: unpack the dependency via the Maven Dependency Plugin and its unpack goal: an unpacked folder will be created to the tmp folder unpack-tmp
Second step: copy the concerned resource via the Maven Resources Plugin and its copy-resources goal.
Third step: re-pack the whole via the Maven Jar Plugin and its classic jar goal
Running the sample above, the junit-modified.jar file will appear in the target folder of the maven project.
The order of the plugin configurations above is important to respect the steps flow as part of the same phase.
Then, you have one additional file to add to your fat-jar, which is indeed the modified jar you were probably looking for.
If you don't need a dynamic approach, a better approach would be to do it once and have a classified version of that dependency as explained in this other SO post.
Alternatively, move it to a Maven profile so that at least it is not part of the default build.
It seems that you're trying to modify a dependency of the maven project before packaging it into the final artifact of the project.
This is dodgy. If that jar/library (the dependency) is yours, then modify the pom of that jar, rebuild it, and then have the current project resolve the latest artifact.
If, otherwise, the dependency jar is not yours, then it should suffice to have the resource (prj-pakacge-module/src/main/resources/file.xml) in the current project (the one you're building now), as the end result will be the same (as long as the assembly plugin is set to flatten all artifact jars in the target uber/fat jar...