Unused classes not packaged in a Maven multi-module project - java

I'm using Maven 3.6.2 with a multi-module Java project. The example module depends on the core module, so I have a dependency in the example module's POM like this:
<dependency>
<groupId>com.company</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
The issue is that not all of the classes from the core module are being packaged into the example jar when I run mvn clean install. Only the classes that are used in the example project are being packaged. I need all classes packaged for a later process.
I'm using the maven-shade-plugin with this configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>module-info.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
How can I include all classes from the core module in the example jar without having to use them in the example jar?

The issue was the minimizeJar parameter. According to https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#minimizeJar:
When true, dependencies will be stripped down on the class level to
only the transitive hull required for the artifact.
I removed that parameter and it worked as expected.

Related

Excluding Classes using Maven Shade Plugin not working

The classes added to exclude in maven shade plugin don't get excluded.
My pom.xml has one dependency which has shaded classes conflicting with other dependencies.
So I added the path to those classes in maven shade plugin to exclude them.
I have this for maven shade plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<excludes>
<exclude>io.confluent.shaded.kafka.serializers.subject.*</exclude>
<exclude>io/confluent/shaded/kafka/serializers/subject/*</exclude>
</excludes>
</filter>
</filters>
<mainClass>com.expediagroup.das.businessevent.CommerceBusinessBookingEventMetricCalculatorApp</mainClass>
<layout>JAR</layout>
</configuration>
</execution>
</executions>
</plugin>
After compiling it still shows those classes not being removed.
Any help is appreciated.

Maven shaded plugin replacing persistence.xml

during maven package i have a strange behavior. I have an artifact "A" that depends from "B".
Within "A" i have a folder META-INF that contains a persistence.xml, also in "B" I have a folder that META-INF with a persistence.xml.
This is pom's file of A relative to shade plugin and dependencies
<dependency>
<groupId>com.mycopany</groupId>
<artifactId>B</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
How can I exclude the persistence.xml file from "B", considering that "B" is also a shaded artifact?
use a second filtre and exclude persistence.xml like this :
<filter>
<artifact>B</artifact>
<excludes>
<exclude>path/persistence.xml</exclude>
</excludes>
</filter>
more details is in this question
and in the official documentation
One solution I found is to Create JPA EntityManager without persistence.xml configuration file.
I have used PersistenceUnitInfo.
Here is the original thread : Create JPA EntityManager without persistence.xml configuration file

Maven:While creating shaded jar, getting warning -The POM for org.knowhowlab.osgi:sigar:jar:1.6.5_01 is invalid

I am creating a shaded jar with Maven shade plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
The shade plugin extracts the classes from all jars and pack them in a single jar but I get following warning for 1 jar:
[WARNING] The POM for org.knowhowlab.osgi:sigar:jar:1.6.5_01 is
invalid, transitive dependencies (if any) will not be available
and the sigar jar gets added as a jar,unlike others, which I was not expecting.
The sigar jar is my pom is added like this:
<dependency>
<groupId>org.knowhowlab.osgi</groupId>
<artifactId>sigar</artifactId>
<version>1.6.5_01</version>
</dependency>
One difference I see in this sigar jar is that it contains files such as .so, .dll.
How can I make this jar get added like others in shaded jar?

How to create Jar file from Maven project in eclipse

I have a Maven project, but I am not familiar to Maven. I wanted to create an executable JAR file from this Maven project to use it in another project by eclipse. How can I do this?
To build jar From Eclipse, Right click on your maven project name then
Run as > Maven install
Right click maven project,
choose Run As-> Maven Build ....
Type package in the Goals box.
Click Run.
Command line approach:
In the root of the project (the maven project), should be a pom.xml. Go to that root and run mvn package. If this is correct, there should be a new folder with the name target in the root of the project. Inside this folder there should be the jar file.
First of all, you have to remember about security in Java. Many jars would not work in fatjars, if they got included in other projects (for example bouncycastle).
If you are doing a simple executable jar that has no libs in it, and requires all of them on classpath, default build (when packageing tag is set to jar) would be ok, and just require a proper manifest.
If you need all libs inside (fatjar), you need to configure it yourself.
There are several plugins for it, for example maven-shade-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/*.INF</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>my.package.MainClass</Main-Class>
<Class-Path>.</Class-Path>
</manifestEntries>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>fat</shadedClassifierName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
Add following into pom.xml file and Run as Maven Install. This worked for me.
pom.xml
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.pohan.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now Run as Maven Install.
if you want with the terminal to do it type
mvn package
and a line before "BUILD SUCCESS" the directory of the
Install maven - https://maven.apache.org/download.cgi
Goto your project in eclipse
Run -> Maven install

Build a "light" jar on multi-module project

I have a multi-module project which I assemble with the assembly plugin to a fat jar. Thank works fine so far, but now I want to build another jar that only consists of special packages of the dependencies of the uber-pom.
An example:
I have 3 deps on sub-projects and I want to have a jar with
com.mycompany.api.*,
com.mycompany.settings.*
com.mycompany.public.*
but not
com.mycompany.internal.*
These packages are distributed through the 3 deps.
Any chance to realize something like that with the assembly plugin?
Thanks,
Jan
The Shade plugin should probably be able to do something like that.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<includes>
<include>com/mycompany/api/*</include>
<include>com/mycompany/settings/*</include>
<include>com/mycompany/public/*</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Categories

Resources