maven shade plugin seems not to use classifier name - maven

When I create a jar file with maven and use profiles with a classifier, the shaded jar file does not have the classifier in the name.
Maven version: Apache Maven 3.2.5
Here is one of the profiles:
<profile>
<id>external</id>
<properties>
<envClassifier>external</envClassifier>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<configuration>
<classifier>external</classifier>
</configuration>
<id>external-package</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
However, the target jar files are not named as I expected (you can see the large shaded jar #111MB doesn't have the classified name of 'external':
363 KB original-myjar-0.1-SNAPSHOT.jar
111 MB myjar-0.1-SNAPSHOT.jar
362 KB myjar-0.1-SNAPSHOT-external.jar
Here is my shading configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>true
</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
---removed
</configuration>
</execution>
</executions>
</plugin>
I could use shadedArtifactId to force the name of the output file but it seems like that shouldn't be needed.
Any ideas?
Thanks

From my experience, the shade plugin takes the primary artifact and replaces it with the shaded one.
If you want the shaded artifact to have a classifier, you should add these configurations to the shade plugin:
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>external</shadedClassifierName>
It's not clear to me why you need the profiles or the additional jar plugin configuration with the classifier. I think you can just use the shade plugin to generate the classified jar that you want. Clarify if there is some requirement forcing you to use profiles.

So I was able to get this working by using the finalName property. Would be curious to here if others use this technique or if there are other solutions.
<profile>
<id>external</id>
<properties>
<envClassifier>internal</envClassifier>
</properties>
<build>
<finalName>${project.artifactId}-${project.version}-external</finalName>
</build>
</profile>
</profiles>

Related

Is it possible to generate a client jar that also includes classes from dependency jars if they are being used in any of the Interfaces?

I am using maven-ejb-plugin with generateClient property set to true, I tried maven-assembly-plugIn but that includes all the libraries that are being used. I only want the classes that are being used from dependency jars in the Interfaces to be included in the client jar.
Is there any setting/configuration in Maven appc (https://docs.oracle.com/middleware/1213/wls/WLPRG/maven.htm#WLPRG614) that I can use to achieve this?
I not sure if what you try to do is a good way but maybe you can use the maven dependency plugin to extract the required classe from your dependent jars.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeTransitive>true</excludeTransitive>
<includes>yourClass.class</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Create shaded and non shaded jars in Maven in same module?

Is it possible to create 2 jars upon maven build:
Shaded jar (uber)
Non shaded jar
I'd like to have both published to maven local. Thanks.
The configuration can be given as follows will create an jar file (done by default) and the following will create a xyz-1.0-shaded.jar supplemental:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

Using bootRepackage=false in Maven

Does anybody know what is the Gradle bootRepackage=false equivalent in Maven? How can you configure spring boot plugin to not generate boot war?
The problem that I face is that I have a multi module project. When I build the project with mvn clean install, the module jar contain the entire libraries defined in its pom.
The solution above applies to older versions. Spring-boot maven plugin 1.2 introduced:
<properties>
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>
Skip the execution. Default value is: false. User property is: spring-boot.repackage.skip.
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/#goals-repackage
and
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/#goals-repackage-parameters-details-skip
You can skip the repackage goal from being executed by setting the skip attribute to true:
Skip the execution. Default: false.
In your plugin configuration, you can then have:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip><!-- true or the result of a Maven/system property for example --></skip>
</configuration>
</execution>
</executions>
</plugin>
It`s works for me
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to manage artifactory / maven artifact using different profiles

i have some problems understanding how to manage different "jar" files generated using different profiles.
An example. I have a project : my.project:sample:1.0
And we use two profiles:
Using Profile1 is compiling using java 7 and with dependency other:project:1.0
Using Profile2 is compiling using java 6 and with dependency different:project:3.0
Using command line in local environment is easy to manage, but I don't know how to store and manage it on artifactory.
We have two different projects that use the same library but generated using different profiles:
A Project require my.project:sample:1.0 - Profile1"
Another project require my.project:sample:1.0 - Profile2"
How can I specify those depedency with the library and the profile?
Clarification: The generated Jar it's storing using artifactory, and the projects resolve the dependencies using the repository. I can't use the project as a module.
You can use classifier to make the distinction between artifacts and reuse the classifier in the dependency.
defining a classifier
To do so you need to define the classifier in maven-jar-plugin:
<profiles>
<profile>
<id>profile1</id>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>jdk6</classifier>
</configuration>
</execution>
</executions>
<plugin>
<plugins>
</build>
</profile>
<profile>
<id>profile2</id>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>jdk7</classifier>
</configuration>
</execution>
</executions>
<plugin>
<plugins>
</build>
</profile>
</profiles>
using the classifier in dependency
Very simple:
<dependency>
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<classifier>jdk6</classifier>
<dependency>

remove jar created by default in maven

I am using maven assembly plugin. in my pom.xml, pakaging type: jar and i dont use maven jar plugin.
Whenever i run mvn clean package, it create 2 jar files: one is from maven assembly, another one is created by default (due to packaging type =jar). I want to keep only the jar file created by assembly plugin only. How to do that?
You may have your reasons but I doubt that it is a good solution to skip the default jar being built and deployed.
Anyhow here is how you can disable the default jar being built.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<id>make-assembly</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- some configuration of yours... -->
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<!-- put the default-jar in the none phase to skip it from being created -->
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources