Maven shaded plugin replacing persistence.xml - java

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

Related

Maven Shade overlapping classes warning

I am using commons-io in my project and want to shade it. I am running into a warning that I can't seem to figure out:
[WARNING] commons-io-2.7.jar, murder-1.0-SNAPSHOT.jar define 180 overlapping classes and resources:
...
I find this strange, as murder-1.0-SNAPSHOT.jar is the jar I am trying to build and which should include the commons-io jar.
I define my commons-io dependency as such:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
<scope>compile</scope>
</dependency>
(I thought I was supposed to use the runtime scope, but then I can't run package because it complains that FileUtils cannot be found)
Here's my config for the shade plugin:
<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>
<artifactSet>
<includes>
<include>commons-io:commons-io</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
If I remove the <filters> entirely, I only get this warning:
commons-io-2.7.jar, murder-1.0-SNAPSHOT.jar define 1 overlapping resource
[WARNING] - META-INF/MANIFEST.MF
Everything still seems to work fine, but I want to get rid of this warning when I package.
EDIT:
If I run mvn clean first, the next mvn package produces no such warning. Subsequent runs however introduces the warning again.
In this end, this is what I ended up with that seems to work and produces no warnings:
<properties>
<!-- replace this with wherever you want your shaded dependencies to end up -->
<shaded-dependencies>io.vapidlinus.shade</shaded-dependencies>
</properties>
....
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>commons-io:commons-io</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.MF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>org.apache.commons.io</pattern>
<shadedPattern>${shaded-dependencies}.org.apache.commons.io</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>

Unused classes not packaged in a Maven multi-module project

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.

How to generate a jar with maven containing specified classes from a project and specified classes from its dependencies?

I would like to generate a jar that contains only few classes from my project and one of its dependencies.
schema of what I would like to achieve
I currently managed to do that with a jardesc file containing the path of all the classes/packages I need since the dependency is also one of my projects.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jardesc>
<jar path="C:/temp/myJar-4.0.0.jar"/>
<options buildIfNeeded="true" compress="true" descriptionLocation="/ProjectB/src/main/resources/exportJar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="true" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
<selectedProjects/>
<manifest generateManifest="true" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true">
<sealing sealJar="false">
<packagesToSeal/>
<packagesToUnSeal/>
</sealing>
</manifest>
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
<javaElement handleIdentifier="=projectA/src\/main\/java<com.stckvrflw.parsingTools"/>
<javaElement handleIdentifier="=projectA/src\/main\/java<com.stckvrflw.utils{ClassA.java"/>
<javaElement handleIdentifier="=projectB/src\/main\/java<com.test.myPackage{ClassE.java"/>
<javaElement handleIdentifier="=projectB/src\/main\/java<com.test.myPackage{ClassF.java"/>
</selectedElements>
</jardesc>
Is it possible to do that with Maven without duplicating classes ?
The jar needs to have it's own artifactId and version.
You can use maven-shade-plugin and configure it like below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<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>
Source: maven-shade-plugin

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?

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