I am using the maven-shade-plugin to combine two seperate jars into a single combined jar.
One of the jars is signed, while the other is not.
If I use the default configuration of the plugin, it will build a broken jar as the new manifest is missing the digest needed by the signature.
I could "fix" the jar by excluding the signature files, but this will of course result in a completely unsigned jar.
I am looking for a way to create a combined jar with all the signed classes remaining signed and valid. - The jar format allows those kind of jars but I could not find an easy way to tell the shade plugin to do so.
Should I write my own transformer to do a proper merging of manifest files or is there already a suitable option in the shade-plugin I did not find, yet?
Example:
pom.xml (defining two modules "foo" and "bar")
<?xml version="1.0"?>
<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>mygroup</groupId>
<artifactId>myparent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>foo</module>
<module>bar</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
foo/pom.xml: (combine signed bar into unsigned foo)
<?xml version="1.0"?>
<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>
<parent>
<groupId>mygroup</groupId>
<artifactId>myparent</artifactId>
<version>1.0</version>
</parent>
<artifactId>foo</artifactId>
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>Foo</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
bar/pom.xml: (create signed bar)
<?xml version="1.0"?>
<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>
<parent>
<groupId>mygroup</groupId>
<artifactId>myparent</artifactId>
<version>1.0</version>
</parent>
<artifactId>bar</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<keystore>${user.home}/keystore-name</keystore>
<alias>alias-name</alias>
<storepass>123456</storepass>
</configuration>
<executions>
<execution>
<id>sign</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If both modules contain a single hello-word class, I would expect java -jar foo/target/foo-1.0.jar to work and jarsigner -verify foo/target/foo-1.0.jar to tell about the presence of signed classes.
The maven shade plugin does not play nicely with signed jars. I would recommend you take a look at Capsule that is way better to do this kind of job.
Related
lets say i have 3 packages and a jar needs to be created for every package containing only the contents in the current package. My attempt is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>first-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>first-jar</classifier>
<excludes>
<exclude>/maven.task.3/src/main/java/third/ThirdMain.java
</exclude>
<exclude>/maven.task.3/src/main/java/second/SecondMain.java
</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>second-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>second-jar</classifier>
<excludes>
<exclude>/maven.task.3/src/main/java/first/FirstMain.java
</exclude>
<exclude>/maven.task.3/src/main/java/third/ThirdMain.java
</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
And this does indeed create different jars, however the content inside is the same, meaning the exclude clauses don't work. I tried excluding only the class(relative/absolute path) and only the package. Don't ask why i do this its for homework, it does not make a lot of sense!
This is the way i attempt of doing it, if there is any other more efficient way please feel free to share it with me!
EDIT: Modular structure must not be used, it must be one single project.
Thanks in advance
It's already been answered in the comments but here's an example.
A parent pom:
<?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>com.essexboy</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>jar1</module>
<module>jar2</module>
</modules>
</project>
And 2 child/modules poms, only the artifactId is different:
<?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>
<parent>
<artifactId>parent</artifactId>
<groupId>com.essexboy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jar1</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>
Extra info
I created the parent with the command:
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE
Then created 2 modules with the command
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE
If you must have a single jar project (no modules and parent) you can do it with the shade-plugin:
<?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>com.essexboy</groupId>
<artifactId>double-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<name>double-jar</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>1</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jar1</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/essexboy/App2*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
<execution>
<id>2</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jar2</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/essexboy/App1*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I have accepted the above answer as right since it's the correct way to do it, but if some crazy kid like me tries to do it the wrong way, here's how:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>first-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>first-jar</classifier>
<includes>
<include>first/FirstMain.class</include>
<include>log4j.properties</include>
<include>pictures/12px-Commons-logo.svg.png</include>
<include>textFiles/first.txt</include>
</includes>
</configuration>
</execution>
</plugin>
Notice to use class instead of java, since it uses the compiled extention! As for the other files, they are resources on the classpath
I am trying to find a good example on how you would build multiple zip files for a multi-module project with several runnable components using the maven-assembly-plugin.
My end result would hopefully have a single "dist" component that is capable of building all the necessary zip files.
<?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>
<parent>
<groupId>xyz.exampleproject</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<name>Project Distribution</name>
<artifactId>dist</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>component1-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>component2-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-bundles</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I would add one extra module per zip file that you want to create and configure the assembly plugin in there.
I have project (TeamCity plugin), written in Scala, packaged by maven and running on TeamCity(which uses Spring).
There is the dependency com.ullink.slack.simpleslackapi in my project. Now I try to update it (from 0.5.2 to 1.2.0) and become an error at runtime.
Error java.lang.NoClassDefFoundError: Could not initialize class
com.ullink.slack.simpleslackapi.impl.SlackWebSocketSessionImpl
The code that caused this exception is pretty simple:
val session = SlackSessionFactory.createWebSocketSlackSession(config.oauthKey)
My build/pom.xml file looks like
<?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>
<parent>
<artifactId>***</artifactId>
<groupId>***</groupId>
<version>1.0.0</version>
</parent>
<artifactId>build</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>***</groupId>
<artifactId>***-server</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>***</finalName>
<outputDirectory>${project.parent.build.directory}</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>plugin-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I've tried to append this dependency to this file and tried maven-shade-plugin — it does not help.
Could you advice what else can I try?
Any code referenced below is from the project here
For the AWS Lambda, I have been trying to create a jar file based on the maven configuration defined here.
<?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>be.quodlibet</groupId>
<artifactId>HelloLambda</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
When I try and build the fat JAR, upload it to AWS Lambda console, I see the error
The following package be.quodlibet.hellolambda was not added to the jar resulting in a
"errorMessage": "Class not found: be.quodlibet.hellolambda.helloHandler",
"errorType": "java.lang.ClassNotFoundException"
As #khmarbaise said in the comments, make sure you run "mvn clean package" and not "mvn clean assembly:single".
We have a multi module maven project where the sonar-maven-plugin and jacoco maven-plugin are defined in the parent pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
<excludes>
<exclude>**/generated/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
Now we have a submodule common, containing again other submodules:
In the submodule common, the maven structure is as follows:
<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">
<parent>
<groupId>be.groupname</groupId>
<artifactId>artifactname</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>be.groupname</groupId>
<artifactId>common-master</artifactId>
<name>Common - Master</name>
<packaging>pom</packaging>
<modules>
<module>bindings</module>
<module>core</module>
<module>tools</module>
</modules>
</project>
The submodule core has the following maven structure:
<?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">
<parent>
<groupId>be.groupid</groupId>
<artifactId>common-master</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common-core</artifactId>
<name>Common - Core</name>
<packaging>jar</packaging>
...
</project>
Now my question, in my SonarQube report, I see the uncovered lines:
The common master project contains all the uncovered lines from the submodules (common-core,...). I would expect that only the common core module would contain those lines.
In the filesystem, I see a jacoco.exec is generated under the common/target folder.
In the subdirectory common, I don't see this file being created but instead I see a file common/core/target/sonar/jacoco-overall.exec