I'm having difficulty using the Maven Shade Plugin because I would like to have my shaded jar be installed to the same folder as the Parent pom (instead of the local src/target
directory).
The layout: maven_project
guide/
parent_pom.xml
projA/
pom.xml
projB/
pom.xml
/target
original-projB-0.0.3.jar
projB-0.0.3.jar (shaded jar)
I have to export the project and to make it easier for others to run the executable jar I want to relocate the shaded jar to the guide
folder.
Unfortunately, I tried using
<outputDirectory>/home/name/Desktop/maven_project/guide/</outputDirectory>
but this only moved the original-jar to the directory.
Question: Any ideas on how to move the shaded jar there instead (and even delete the original jar in the process)?
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
The dependency-reduced-pom. xml removes transitive dependencies which are already in your shaded jar. This prevents consumers from pulling them in twice.
The Maven Shade Plugin by default replaces the original jar generated by the build and creates a copy of it prefixed with original.
Replacement and relocation can be configured via the outputDirectory
, outputFile
and finalName
configuration entries.
Applying the configuration below:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-${project.version}-something</finalName>
<outputDirectory>../guide</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
We are:
guide
folder (via relative path, better approach as also suggested by @Tunaki)finalName
element in order to disable replacement (which also affects relocation, in the sense that also the (prefixed) original jar will be relocated). As per official documentation the finalName is
The name of the shaded artifactId. If you like to change the name of the native artifact, you may use the
<build><finalName>
setting. If this is set to something different than<build><finalName>
, no file replacement will be performed, even ifshadedArtifactAttached
is being used.
As such, Maven will generate only the shaded jar at the configured location.
Another approach, would be to use the outputFile
configuration entry, which specifies:
The path to the output file for the shaded artifact. When this parameter is set, the created archive will neither replace the project's main artifact nor will it be attached. Hence, this parameter causes the parameters
finalName
, shadedArtifactAttached,shadedClassifierName
andcreateDependencyReducedPom
to be ignored when used.
Hence you could change the configuration above to:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>../guide/${project.artifactId}-${project.version}-shaded.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And have exactly the same behavior.
Side note: you are really changing the behavior of the build here. If somebody builds only a single module, from the module folder itself, he/she would not find the expected content on the target
folder, which would be on the parent one instead (a bit of a surprise).
Update
Applying the configuration above and invoking only the Shade Plugin from the command line
mvn shade:shade
You will however have the following issue:
[INFO] --- maven-shade-plugin:2.4.3:shade (default-cli) @ test-addjar ---
[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR] supported. Please add the goal to the default lifecycle via an
[ERROR] <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR] remove this binding from your POM such that the goal will be run in
[ERROR] the proper phase.
[ERROR] - You removed the configuration of the maven-jar-plugin that produces the main artifact.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With