Manifest in maven-shade-plugin is not working - java

Goal: Create build number and put it in the manifest generated by maven-shade-plugin. Then, read that build number.
I have used ManifestResourceTransformer and declared manifestEntries there.
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<!-- Run shade goal on package phase -->
<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>app.MainApp</mainClass>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</transformer>
</transformers>
<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>
Build number is correctly generated from the resulting log of mvn package.
Then, I read the generated manifest:
Manifest mf = new Manifest();
mf.read(getClass().getResourceAsStream("/META-INF/MANIFEST.MF"));
Attributes attr = mf.getMainAttributes();
System.out.println("Manifest-Version : " + attr.getValue("Manifest-Version"));
System.out.println("Created by : " + attr.getValue("Created-By"));
System.out.println("Built by : " + attr.getValue("Built-By"));
System.out.println("Implementation-Build: " + mf.getEntries().get("Implementation-Build"));
Result
Manifest-Version : 1.0
Created by : 1.6.0_65-b14-466-11M4802 (Apple Inc.)
Built by : null
Implementation-Build: null
For good measure, I even hardcoded the build number.
<manifestEntries>
<Implementation-Build>123123</Implementation-Build>
</manifestEntries>
The result still the same.
Thoughts: The manifest really looks like the DEFAULT manifest instead of anything.
Edit: My pom file:
<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>MyGroupId</groupId>
<artifactId>MyArtifactId</artifactId>
<name>MyApp</name>
<scm>
<connection>scm:git:https://github.com/myorg/myrepo.git</connection>
<developerConnection>scm:git:https://github.com/myorg/myrepo.git</developerConnection>
<tag>DEVELOP</tag>
<url>https://github.com/myorg/myrepo.git</url>
</scm>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>app.MainApp</mainClass>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</transformer>
</transformers>
<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>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<format>{0,date,yyyyMMddHHmmss}</format>
<items>
<item>timestamp</item>
</items>
<doCheck>true</doCheck>
<doUpdate>true</doUpdate>
</configuration>
</plugin>
<plugin>
<!-- Deploy the web site -->
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.9</version>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>site-deploy</phase>
<configuration>
<!-- must match the server's id -->
<server>github</server>
<!-- The commit message -->
<message>Building site for my project</message>
<!-- The location where the site is uploaded -->
<path>${site.path}</path>
<!-- Use merge or override the content -->
<merge>true</merge>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>4thline-repo</id>
<url>http://4thline.org/m2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
...
</dependencies>
</project>

how do you run the attached code? If you are running it as a test, it will produce your output because the test classpath does not include the MANIFEST.MF file. You will need to include the generated (+shaded) jar in your classpath for that code to run.

Related

Maven-shade-plugin has detected that some class files are present in two or more JARs

I'm trying to use the maven-shade-plugin but I get a warning:
javafx-controls-18.0.1-win.jar, javafx-graphics-18.0.1-win.jar,
javafx-media-18.0.1-win.jar, javafx-web-18.0.1-win.jar define 1
overlapping resource:
META-INF/substrate/config/resourcebundles javafx-graphics-18.0.1-win.jar, javafx-media-18.0.1-win.jar,
javafx-web-18.0.1-win.jar define 2 overlapping resources:
META-INF/substrate/config/jniconfig-x86_64-linux.json
META-INF/substrate/config/reflectionconfig-x86_64-linux.json javafx-base-18.0.1-win.jar, javafx-controls-18.0.1-win.jar,
javafx-graphics-18.0.1-win.jar define 1 overlapping resource:
META-INF/substrate/config/reflectionconfig.json maven-shade-plugin has detected that some class files are present in two or more JARs.
When this happens, only one single version of the class is copied to
the uber jar. Usually this is not harmful and you can skip these
warnings, otherwise try to manually exclude artifacts based on mvn
dependency:tree -Ddetail=true and the above output. See
https://maven.apache.org/plugins/maven-shade-plugin/
This watning is subject to change (possibly from my actions).
I tried to fix it with this, but it didn't work:
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*</exclude>
</excludes>
</filter>
</filters>
I began to try to fix this warning due to the fact that shade-jar does not start due to an error:
Error: JavaFX runtime components are missing, and are required to run
this application
Please tell me how to fix this error and run shade-jar?
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>TEST</artifactId>
<version>1.0</version>
<name>TEST</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.8.2</junit.version>
<javafx.version>18.0.1</javafx.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj-javafx</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.test.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<source>18</source>
<target>18</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<mainClass>com.test/com.test.Main</mainClass>
<launcher>TEST</launcher>
<jlinkZipName>TEST</jlinkZipName>
<jlinkImageName>TEST</jlinkImageName>
<noManPages>true</noManPages>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
<compress>2</compress>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I used this instead "maven-shade-plugin"
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Launcher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
And this: https://stackoverflow.com/a/70809214/10946427

Maven Build is failing

This is the error.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project DropwizardWithFlipkart: Compilation failure
[ERROR] /Users/aditya.verma/Desktop/DropwizardWithFlipkart/src/main/java/com/flipkart/aditya/dropwizard/DropWizardApplication.java:[29,6] reached end of file while parsing
here is my pom.xml file
<?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>DropwizardWithFlipkart</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<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.flipkart.aditya.dropwizard.DropWizardApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

How do you package a GWT application in a fat JAR using maven?

My GWT application is currently packaged into a war using maven, which is then deployed to tomcat. I'd want to change to packaging in a jar so it can be executed with java -jar, and have the application served from an embedded web server like Jetty. Similar to what happens when you run in dev mode, but I want it to happen from a single easily deployable fat/uber jar including html and GWT js files. I've only been able to find instructions for doing this for Jetty with spring, and haven't been able to adapt this for GWT. Something like Jetty's example uber jar project is close, just needs to work with GWT as well.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.project</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
for more info
You can simply change the packaging of your maven project:
<project>
...
<packaging>jar</packaging>
...
</project>
Please note that you couldnt deploy a jar file in tomcat.
Next I would make sure that the dependency gwt-dev has scope provided (since this dependency contains already an old version of jetty). After that you could add the jersey dependency and start an embedded server which serves your GWT-Appliccation (for example see here).
Combining the example Jetty project and #mam10eks answer, I think I've managed to figure it out for a simple GWT app anyway:
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.HelloJettyGwt</groupId>
<artifactId>HelloJettyGwt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloJettyGwt</name>
<properties>
<gwtVersion>2.7.0</gwtVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty-version>9.3.2.v20150730</jetty-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>${gwtVersion}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
<version>${jetty-version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
<goal>generateAsync</goal>
</goals>
</execution>
</executions>
<configuration>
<runTarget>app.html</runTarget>
<modules>
<module>com.example.HelloJettyGwt.app</module>
</modules>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>
src/main/webapp
</directory>
<filtering>true</filtering>
<includes>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/web.xml</include>
</includes>
</resource>
<resource>
<directory>
${project.build.directory}/${project.build.finalName}/
</directory>
<filtering>true</filtering>
<includes>
<include>**/app/**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.HelloJettyGwt.ServerMain</mainClass>
</manifest>
<manifestEntries>
<Implementation-Version>${jetty-version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>rebuild-war</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/VERSION.txt</exclude>
<exclude>META-INF/LICENSE.txt</exclude>
</excludes>
</filter>
<!-- exclude the MANIFEST.MF entries that will cause problems -->
<filter>
<artifact>javax.annotation:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>javax.servlet:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>javax.websocket:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>org.eclipse.jetty:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>org.eclipse.jetty.websocket:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>org.ow2.asm:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.0.0,)</version>
</requireMavenVersion>
<requireJavaVersion>
<version>[1.8,)</version>
<message>[ERROR] OLD JDK [${java.version}] in use.
Jetty
${jetty-version} requires JDK 1.8 or newer</message>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
main method:
Server server = new Server(8080);
URL webRootLocation = this.getClass().getResource("/app.html");
if (webRootLocation == null)
{
throw new IllegalStateException("Unable to determine webroot URL location");
}
URI webRootUri = URI.create(webRootLocation.toURI().toASCIIString().replaceFirst("/app.html$","/"));
WebAppContext altHandler = new WebAppContext();
altHandler.setResourceBase(Resource.newResource(webRootUri).toString());
altHandler.setDescriptor(Resource.newResource(webRootUri).toString() + "WEB-INF/web.xml");
altHandler.setContextPath("/");
altHandler.setParentLoaderPriority(true);
server.setHandler(altHandler);
server.start();
server.join();
}

How to run a java testNg project's jar in server?

I created a java-testNg project in eclipse.I had another java spring project in which i want to upload that testNg project to sever folder and run it from server.
On upload time i created the jar of testNg project and i am uploading that jar.Can anybody help how to run that testNg project on server...any ideas....plz help
You can run TestNG from the command line.
You can execute command line statements from Java using a ProcessBuilder.
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">
<packaging>jar</packaging>
<properties>
<!-- the main function-->
<start-class>org.kuohai.TestNGMain</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
<build>
<finalName>packageName</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter,
org.uncommons.reportng.JUnitXMLReporter
</value>
</property>
</properties>
<forkMode>always</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${start-class}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is the main function
package org.kuohai;
public class TestNGMain {
public static void main(String[] args) {
org.testng.TestNG.main(args);
}
}
then you can use mvn clean package -DskipTests
Package and run
java -jar packageName.jar -testjar packageName.jar
If you have other questions, you can run java -jar packageName.jar You can see the message

Maven shade plugin does not exclude the manifest signature files

I am using the maven shade plugin to generate a consolidate jar for my project. The jar is generated as expected and when i try to use the jar and run it, i get a
java.lang.SecurityException: Invalid signature file digest for
Manifest main attributes error.
I googled the above error message and many people have suggested to exclude the manifest signatures from the META-INF directory. Thus i have included the step to exclude these files from the directory [i see two files by the name JARSIGN_.RSA and JARSIGN_.SF], but for some strange reason, maven shade plugin is unable to exclude these files from the META-INF directory. Could anyone explain me what i might be doing wrong? My pom.xml is below and the command that i am using to generate the jar is:
mvn clean package shade:shade
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.abc.xyz</groupId>
<artifactId>myjar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<!-- A few custom properties -->
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<!-- Other The dependencies are here -->
</dependencies>
<repositories>
<!-- Repository Information -->
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<!-- The below statement is not executed by shade plugin -->
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>com.google.guava:guava</include>
<include>com.google.code.gson:gson</include>
</includes>
</artifactSet>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.abc.xyz.HelloWorld</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
With shade plugin 3.2.1 the following works for me.
<!-- language: lang-xml -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
On the plugin's doc page (https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html) the whole <configuration> block is shown inside the <execution> tag. This does not work. The <configuration> block should be outside the <executions> tag as shown above.
Maybe the configuration syntax for the plugin has changed, but this worked for me in the past with version 1.5 of the shader plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>META-INF/JARSIGN_.SF</resource>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
I have not tried to use wildcards. But looking at the documentation the following should exclude all .SF files:
<resource>.SF</resource>
See this thread for another example.
I had a similar issue where the Shade plugin apparently did not exclude files from META-INF directory no matter what settings I tried. I was checking it using the following bash commands:
mvn clean install
7za x target/built-jar-6.4.0.jar -aoa -o/tmp/unpacked/
ls /tmp/unpacked/META-INF/
The problem was actually not in the shade plugin itself, but in the way I was unpacking the JAR - the unpacking command overwrites files, but it keeps the old files in place, making me think there was something wrong with my settings.

Categories