maven-shade-plugin won't include dependencies - maven

I have an infuriating problem. I am trying to use maven-shade-plugin to pack my spring mvc app with an embedded tomcat server into a single jar. I've already tried a similar task. Tried to use a similar configuration in my pom file as once worked for me. Unfortunately after packing the jar only contains the classes I have created. The manifest does mention all the dependencies. I thought that this might be a problem with Maven, but tried on another project, and the plugin seems to work fine. Here are relevant fragments of my POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wojto</groupId>
<artifactId>wmcase</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WMcase Maven Webapp</name>
<properties>
<springframework.version>5.2.3.RELEASE</springframework.version>
<springsecurity.version>5.1.4.RELEASE</springsecurity.version>
<tomcat.version>9.0.30</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
(...)
<build>
<finalName>wmcase</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.wojto.wmcase.application.Application</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.wojto.wmcase.application.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

Remove the tag <pluginManagement></pluginManagement> and keep the rest than this will work.
The trick is simply cause pluginManagement is as the name implies for the management of the plugins which is usually the version and sometimes configuration. This means plugins define in pluginManagement will never being executed.
If you like to have plugins which being executed use <plugins></plugins>
see also the documentation: http://maven.apache.org/pom.html

Related

Command line mvn missing dependency

I have a simple program. From the main, I call a URL and it opens. When I run it in intellij, it works fine, but the command line doesn't work.
Also, when I create another java program and import this jar, I get the same error.
This is my POM.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>XMLConverter</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>
</project>
The dependency is missing
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/XML
at xmlconverter.GetXmlData.ReadString(GetXmlData.java:50)
at xmlconverter.GetXmlData.getJson(GetXmlData.java:24)
at xmlconverter.XMLConverter.main(XMLConverter.java:13)
Caused by: java.lang.ClassNotFoundException: org.json.XML
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 3 more
When I run this : mvn dependency:tree I get the right dependency.
My assumption is because I have two .java files. I have a main and then the code. It worked fine when it was all one file XMLConverter.
https://www.dropwizard.io/en/latest/getting-started.html#building-fat-jars
Add the following plugin also along with you maven-compiler plugin, to create the jars, mention your main class path under the mainclass tag below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example. <Path to main class></mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins></build>

How to reasonable rename package with maven plugin

My project has the following modules:
client
rest
The above modules both depend on com.google.protobuf, and rest depends on client (the rest module uses protobuf jar by client).
In order to avoid conflict, I renamed com.google.protobuf to my.com.google.protobuf in client module with shade plugin.
The problem is that the rest module can not be compiled and reports the following error:
error: incompatible types: my.com.google.protobuf.Descriptors.FileDescriptor cannot be converted to com.google.protobuf.Descriptors.FileDescriptor
Client pom.xml
<parent>
<groupId>my-project</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>client</artifactId>
<name>Client</name>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>client</artifact>
<includes>
<include>**/*.class</include>
</includes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.my.client.Client</mainClass>
</transformer>
</transformers>
<relocations>
<relocation>
<pattern>my.google</pattern>
<shadedPattern>shiva.com.google</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
rest pom.xml
<parent>
<groupId>my-project</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>rest</artifactId>
<dependencies>
<dependency>
<groupId>my-project</groupId>
<artifactId>clientt</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>rest</artifact>
<includes>
<include>**/*.class</include>
</includes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.my.rest.WebServer</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
A better workaround seems to be: Right-click on project "client" -> pom.xml in the project view in IntelliJ, choose "Maven" -> "Ignore Projects". Then do a "Maven" -> "Reimport" on the top-level pom.xml.

How to set values for properties tag of maven by reading the .properties file

I have 5 projects, here I want to copy the java classes from one non-maven project to maven project for this requirement I used maven-ant-plugin. with this I am able to do the copy successfully.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project1_maven.src.main.java>c:/maven1/src/main/java</project1_maven.src.main.java>
<project1_maven.src.test.java>c:/maven1/src/test/java</project1_maven.src.test.java>
<project1.nonmaven.src>c:/non-maven1/src</project1.nonmaven.src>
<project1.nonmaven.test>c:/non-maven1/test</project1.nonmaven.test>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project1.nonmaven.src}"
todir="${project1_maven.src.main.java}" />
<copy file="${project1.nonmaven.test}"
todir="${project1_maven.src.test.java}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
</project>
But here my question is, instead of giving the source and destination paths (I want to give this like for remaining 4 projects) inside the pom.xml directly, I just want to read it from the properties file.
for example :
<project1.nonmaven.src>
${get the location from property bundle by using key}</project1.nonmaven.src>
Can you please some one suggest me how ??
Thanks.

Maven build sub projects using root pom

I have different independent maven projects and want to build it with single pom. So I am using following pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a.b</groupId>
<artifactId>my-sdk</artifactId>
<name>my-sdk</name>
<version>1.0</version>
<packaging>pom</packaging>
<description>Generating SDK</description>
<modules>
<module>project1</module>
<module>project2</module>
<module>project3</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>process-sources</id>
<phase>process-classes</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="init" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jboss-repo</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>
I have the following questions:-
Javadoc is not generating, so where I am doing wrong. If I run "mvn javadoc:javadoc package" command it generates java doc. But if I run "mvn package" it doesn't.
In project2 there is assembly plugin to generate fat jar, it generates jar of project2 but unable to generate fat jar. If I build project2 independently it generate fat jar successfully.
Follwing section is from project2 pom to generate fat jar which works successfully if I run the build independently
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<configuration>
<archive>
<manifest>
<mainClass>[MAIN CLASS]</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any Suggestions.
I'd say check the documentation for both of your questions ;-)
javadoc (http://maven.apache.org/plugins/maven-javadoc-plugin/plugin-info.html)
javadoc:jar and javadoc:javadoc should not be used for an aggregator project, use javadoc:aggregate or (in your case) javadoc:aggregate-jar
assembly (http://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html)
first, assembly:assembly is deprecated and second, it should only be used on the command line. I guess using goal single will help.

Adding java source (.java files) to test jar in Maven

I'm making use of my pom.xml and am was able to generate the jar for src/main/java (say app.jar) as well as for src/test/java (say app-test.jar). I was also able to include my java sources as part of the app.jar (i.e. have both my .class as well as my .java files in the jar).
However for my app-test.jar, i'm not able to include my .java files in it.
This is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Any help would be appreciated.
Thanks.
Update on post on Whaley's suggestion:
Tried the maven-antrun-plugin, but rt now after running mvn package all i'm getting inside my tests.jar is the META-INF folder. .java and .class are not getting included:
This is the part of the pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.testOutputDirectory}">
<fileset dir="${project.build.testSourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Thanks.
I think you probably need to generate a custom assembly using the assembly plugin:
http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html
But a much cleaner solution would be to package test sources separately using the source plugin
http://maven.apache.org/plugins/maven-source-plugin/test-jar-mojo.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-test-sources</id>
<goals>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
you can then have both the test source jar and the test class jar on your classpath and do something like this:
YourClassName.class.getResource("YourClassName.java");
or more generically:
public static InputStream getSourceForClass(final Class<?> clazz) {
final String baseName = clazz.getSimpleName();
return clazz.getResourceAsStream(baseName + ".java");
}
to access the sources
The includes property of the jar plugin only allows the inclusion of files from a relative path from the plugin's classesDirectory property. So that won't work unless you copied your .java files to ${project.build.outputDirectory} somehow. You could do something like that with the maven-antrun-plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.outputDirectory}">
<fileset dir="${project.build.SourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources