I'm using assembly plugin to package a list of applets into zip in one of modules with my maven project. here is the pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>applets-deploy</artifactId>
<name>deploy</name>
<dependencies>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>applets-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>ceplot-applet</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
......
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/resources.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
what I need is to also sign jars before they are packaged, can I use jarsign plugin here, and how? I can't find where the jar files are temporarily stored during the build process.
Thanks,
This would be a pretty normal use of the jarsigner plugin. By default, jars are built by the jar plugin during the package phase and output to the ${project.build.directory}, which defaults to target.
You'd just need to sign the jars some time after they're built during package and before you assemble the zip. You could do that by binding the assembly plugin to a later phase or by adding the jarsigner plugin above the assembly plugin and binding it to the package phase, too.
Related
I have the following 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>org.example</groupId>
<artifactId>do_can_proj</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11-production</artifactId>
<version>21.1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.do.can.Main</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>
</pluginManagement>
</build>
</project>
Running,
$ mvn clean package assembly:single
generates a jar in the target folder.
When I run the jar file from the command line:
java -jar target/do_can_proj-1.0-SNAPSHOT-jar-with-dependencies.jar
I get the following error:
SQL State: 08001
No suitable driver found for jdbc:oracle:thin:#hostname:1521:SID
And this is how I have my project setup:
When running with the -jar argument, the classpath is set to the entry in the manifest in the jar file. By default this is empty. It can be argued that the assembly target should be improved to allow this.
It needs to be set to include all libraries used by your code for this to work and the libraries needs to be added from the Maven repository. How exactly to do this depends on your needs. See How can I create an executable JAR with dependencies using Maven? for suggestions.
Normally we use dependency tag in pom.xml to have Maven include a JAR on the classpath and it also packages that dependency.
What if the JAR is generated in a step of the same project's build? I mean, it's not the compile plugin that generates it, it is a JAR without any java source and an external executable creates it.
I can use maven exec plugin to have my JAR generated and maven install plugin to have it installed to my local repository. But still I can't have it as a dependency in the same project: No matter which phase I put my JAR generator command in, the dependency check will happen before that and fail because the JAR does not yet exist.
Is system scope dependency my best choice? Then I need to give up packaging. And it's deprecated. And the JAR needs to be outside the project directory.
Or the JAR generator must be in a separate pom? Also not very nice because the JAR is only used by this one project.
Can I configure the dependency plugin to defer the dependency check and download to compile phase?
Any other solution?
This pom almost works, but first time I need to install the generated thing manually to the repo.
<?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>
<artifactId>something-parent</artifactId>
<groupId>something</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>something-sample</artifactId>
<properties>
<jnbridge.path>C:/Program Files (x86)/JNBridge/JNBridgePro v9.0</jnbridge.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-proxies-jar</id>
<phase>generate-test-resources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>jnbproxy.bat</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>jnbridge.local</groupId>
<artifactId>proxies</artifactId>
<version>0.0.0</version>
<packaging>jar</packaging>
<file>${basedir}/proxies.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!--
Yes, these are true system-scope dependencies. JNBridge is expected to be
installed on the system wherever this project is built.
-->
<dependency>
<groupId>com.jnbridge.org.apache</groupId>
<artifactId>bcel</artifactId>
<version>5.1</version>
<scope>system</scope>
<systemPath>${jnbridge.path}/jnbcore/bcel-5.1-jnbridge.jar</systemPath>
</dependency>
<dependency>
<groupId>com.jnbridge</groupId>
<artifactId>jnbcore</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${jnbridge.path}/jnbcore/jnbcore.jar</systemPath>
</dependency>
<!--
This one will be installed to local maven repo in the process-test-resources phase,
as defined in the project/build/plugins section of this file.
-->
<dependency>
<groupId>jnbridge.local</groupId>
<artifactId>proxies</artifactId>
<version>0.0.0</version>
</dependency>
<!-- other dependencies... -->
</dependencies>
</project>
I'm on a project working with openrdf, and I require the shade plugin to transform my service entries. I would like to build a war and a jar simultaneously, as both usages are possible. However, I cannot get the shade plugin to produce a shaded jar and a shaded war at the same time - shade only invokes on the package type defined in the properties, and binding e.g. the jar plugin to the package phase in order to create a jar next to the war results in an unshaded jar. How can I create both a shaded jar and a shaded war at the same time?
If by "shaded war" you mean just the regular war with all dependencies packed into WEB-INF/lib, then you might just use maven-war-plugin separately and use jar as packaging type. This way shade plugin will work correctly. And .war will be built by plugin.
Below is pom.xml. And here is working example.
<?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>so.test</groupId>
<artifactId>stackoverflow-test2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.build.finalName}-fatjar</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.14.9</version>
</dependency>
</dependencies>
</project>
I have a maven web application that is using a jar I created and is referenced as a dependency in my pom file.
The jar file I created has another jar file as a dependency.
When I deploy the web application, it says that a class in the dependency of the jar file I created cannot be found.
The only way to work around this was to add the dependency of the jar file as a dependency of the war file. This seems unnecessary.
Is there a way I can configure the war file to be able to see the classes defined in a dependency of a dependent jar file?
my jar pom file looks like: The dependency that has the class that can't be found is the the QRS one.
<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>group</groupId>
<artifactId>artifact</artifactId>
<packaging>jar</packaging>
<version>1.0.7</version>
<name>name</name>
<dependencies>
<dependency>
<groupId>group2</groupId>
<artifactId>QRS</artifactId>
<version>8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
my war pom looks like:
<dependency>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.0.7</version>
</dependency>
and the war plug in is configured:
<plugin>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warName>${project.name}-${project.version}-${env}</warName>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<include>jboss-web.xml</include>
<include>web.xml</include>
<targetPath>/WEB-INF</targetPath>
</resource>
</webResources>
<warSourceExcludes>**/toAggregateAndRemove/**</warSourceExcludes>
<goal>war:manifest</goal>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Maven already resolves transitive dependencies.
That it isn't indicates you're likely not doing it right: the other jar you've created should be a Maven project with its own pom, installed at least locally via mvn install, and listed as a dependency in the second project (the one that uses it).
The project using the jar only needs to specify that artifact as its dependency, the other project's dependencies will be transitively determined and included as a project dependency.
#Dave Newton has answered, this should happen by default.
A few observations from the snippets in the question - if that helps...
Packaging dependant jars as a folder of artifact project's jar.
Defining the maven war plugin jar itself as a dependency for maven war plugin - this is not required at all!
war:manifest goal in the maven war plugin configuration.
It is possible that the war:war which actually creates the war is not run.
I have Project A and Project B. ProjectB just has only proprietary jars or third party libs that ProjectA needs which cannot be found in maven repository. I intended to package all the related ( grouping dependencies) jars of Project B into one/more jar files. Question is, how do I install these jars and shade/assemble them in to one jar at the same time using POM. I am getting proprietary jars installed separately but I cannot get them packaged them in to one jar. M2 eclipse is also not helping much , so I decide to use mvn commands inside eclipse. What is the best practice to do this?. I think shading them since I want to use these dependencies in other projects as well.Can I install and shade/assemble jars( logically group the related dependencies in to one jar) at the time using POM?.
I followed this( from stack over flow forum) , it did not work- as this is creating one project jar which doesn't have anything + proprietary jars separately but not shading all jars in to one. I want them installed and combined in to one jar at the same time.
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.projectdeps</groupId>
<artifactId>sharedlibs</artifactId>
<version>1.0-SNAPSHOT</version>
<name>shared-libs</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>install activationjar</id>
<goals><goal>install-file</goal></goals>
<phase>validate</phase>
<configuration>
<file>${basedir}/src/main/libs/activation.jar</file>
<groupId>com.mycompany.activation</groupId>
<artifactId>activation</artifactId>
<version>12.8</version>
<packaging>jar</packaging>
</configuration>
</execution>
<execution>
<id>install opsjar</id>
<goals><goal>install-file</goal></goals>
<phase>validate</phase>
<configuration>
<file>${basedir}/src/main/libs/Operations.jar</file>
<groupId>com.mycompany.gcs.ops</groupId>
<artifactId>Operations</artifactId>
<version>12.8</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>Operations</artifact>
<includes>
<include>/**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
</project>