I'm building a java service which has two distribution. Each distribution must be build different (one has a spring-boot embedded jetty server inside, the other not). In both, I created a distribution with dependencies, except one (already achieved). The jar without jetty is build using maven-assembly-plugin (similarly as here Problems running executable jar with dependencies) the other one uses spring-boot-maven-plugin (see http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html). My issue is the external dependencies.
I want add an additional dependency on runtime, but I DON'T WANT TO US OSGi
In both building process the result is a 'executable' jar, which I can run like this:
java -jar my.jar
But I don't know how to address the additional dependencies. If I run the command above without a the external dependency (lets say external.jar) inside my.jar will fail, even if is in the same folder. I can make it work for the distribution without jetty like this:
java -classpath "./*" my.main.App
But this doesn't work for my-with-jetty.jar. I also try to run:
java -classpath "./*" -jar my-with-jetty.jar
This simply doesn't work.
So my question are:
is it possible to pack a jar with almost all the dependencies for both cases?
is it possible to pack the jar as runnable jar such that it not necessary to provide the main class?
of course if yes, how? I would like to run both distribution in the same manner.
I want similar behavior:
java -cp "./*" java -jar my.jar conf.cfg
and
java -cp "./*" java -jar my-rest.jar conf.cfg
Thank you.
You need at least 3 maven projects all under the same parent so they get built together and have the same version. All your code will be in the shared project, this will be included in both runnable jar projects. Each runnable jar project will be build differently.
Your parent pom will be something 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>shared-jar</module>
<module>spring-boot-jar</module>
<module>jetty-jar</module>
</modules>
</project>
Your shared-jar project will have all the shared code
Your spring-boot-jar will look like :
<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>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>shared-jar</artifactId>
<version>${project.version}</version>
</dependency>
.... you will need to add all the spring boot dependencies with versions
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Your jetty-jar will be built using the
<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>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jetty-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>shared-jar</artifactId>
<version>${project.version}</version>
</dependency>
.... other dependencies
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</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.bar.Application</Main-Class> -->
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Related
I got a project which consists of two modules.
The first one is Spring Boot REST Web Service
And the second one is the code that should work with this service.
The issue: I need to start Web Service from another module from the code.
Of course, the best option here is to deploy Service to some remote host, but what are the options if I want to launch Service on the local machine?
The first idea was packaging Service module, then copying jar to the second module using maven-dependency-plugin and launching it as:
Runtime.getRuntime().exec("java -jar my-rest-service.jar");
Can I start Spring Boot app right from another module? Call Application.main() method or something?
You can build and install first module into your local maven repository (.m2 folder by default) as the jar library. Then you can use this library in your second module as the maven dependency.
After that you can start your application (second module) as usually spring-boot starts - with the main method.
Example:
First module - library:
<?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>
<url>https://example.com/module1</url>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<name>module1</name>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<java.version>12</java.version>
</properties>
<build>
<finalName>module1</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Second module - application:
<?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>
<url>https://example.com/module2</url>
<groupId>com.example</groupId>
<artifactId>module2</artifactId>
<name>module2</name>
<version>1.0.0</version>
<packaging>jar</packaging>
<!--packaging>war</packaging-->
<properties>
<java.version>12</java.version>
</properties>
<build>
<finalName>module2</finalName>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- com.example -->
<dependency>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
The Spring Boot Maven plugins package our application as executable JARs – such a file can't be used in another project since class files are put into BOOT-INF/classes.
In order to share classes with another project, the best approach to take is to create a separate jar containing shared classes, then make it a dependency of all modules that rely on them.
So in your spring boot module, you need to add classifier like:
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
and of course, you need to add your spring boot module dependency in your pom where you want to use then you should be able to use Application.java.
Please check here: Using a Spring Boot Application as a Dependency.
My goal is to package an application into a modular runtime image bundled with a custom JRE, using jlink. My app is a simple "hello world" Java Standard Edition app, with a dependency to Guava. I use the JDK 11.
Basically I try to reproduce this tutorial by Baeldung, but with NetBeans, Maven to manage the dependencies and the Maven Compiler Plugin version 3.8.1 for the build with the module system.
The directory structure:
The module-info.java file:
module TestwithJLink {
requires guava;
exports net.clementlevallois.testwithjlink;
}
Controller.java:
package net.clementlevallois.testwithjlink;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
public class Controller {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Multiset<String> test = HashMultiset.create();
test.add("hello");
test.add("world");
System.out.println("test: "+ test.toString());
}
}
The 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>net.clementlevallois</groupId>
<artifactId>TestwithJLink</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<id>compile</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
But it creates compiled classes, no jars or modules. So I can't go further (analyze the modules of the jar with jdeps, then using jlink). I must be missing something obvious but what?
If you want to create a JAR file, then go to the root folder containing pom.xml in your terminal and type :
mvn package
This will create a JAR in target folder.
Now change your path in terminal to target folder and Run the JAR file using:
java -jar {file-name-version}.jar
Finally got it. The scenario:
working from NetBeans, with your dependencies handled by Maven
you app has a module-info.java declaration
your dependencies also have a module-info.java declaration.
You want to package your app in a way that respects the modular system. So:
have these 3 Maven plugins listed in your pom (see below). Be careful about the version numbers for the plugins! In particular, the <goal>resolve</goal> in the maven-dependency-plugin makes sure your dependencies are packaged with their module-info.java file, which is not the case otherwise! (see here).
when the compilation is done, move the jar of your app in the lib folder where all the jars of your dependencies are already located.
You can run this app directly with the run icon in NetBeans, or:
from the parent folder of your lib folder, run: java --module-path lib --module NameOfYourModule
The 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>net.clementlevallois</groupId>
<artifactId>TestwithJLink</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>net.clementlevallois</groupId>
<artifactId>utils</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>resolve</goal>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>net.clementlevallois.testwithjlink.Controller</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<id>compile</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
I am trying to create an executable jar for my multi-module maven project. I used the maven-assembly-plugin to generate the jar.
Even though am getting the jar created, am getting the ClassNotFound exception while trying to run the jar file using java -jar command.
Use the shade plugin, much easier than assembly.
Parent pom to hold it all together:
<?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.greg</groupId>
<artifactId>fat-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>library-jar</module>
<module>final-jar</module>
</modules>
</project>
Final build jar pom, with dependency to library:
<?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>fat-jar</artifactId>
<groupId>com.greg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>final-jar</artifactId>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>library-jar</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.greg.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
There is some mistake in you descriptor or how you are using the assembly plugin.
A jar file is like a zip, just open your jar and find your class, if you don't find it then check your pom file or descriptor to understand why.
I am trying to build a single uber-jar from a set a modules that are mostly independent, but it's not working the way I'd thought.
I was initially directed here: https://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/module-binary-inclusion-simple.html though now I'm not quite certain that this was what I should have started with...
The parent pom file looks like this:
<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</groupId>
<artifactId>BigProject</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>BigProject</name>
<modules>
<module>../mod1</module>
<module>../mod2</module>
<!-- ...a bunch more... -->
<module>../distribution</module>
</modules>
<build>
<pluginManagement>
<plugins>
<!-- not even sure why this needs to be specified here, but the documentation seems to think it's important... -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The pom for the "distribution" module looks like this:
<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>
<artifactId>distribution</artifactId>
<name>distribution</name>
<packaging>pom</packaging>
<parent>
<groupId>com.mycompany</groupId>
<artifactId>BigProject</artifactId>
<version>0.0.1</version>
</parent>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>distro-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And finally, the assembly file:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>bin</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<includeSubModules>true</includeSubModules>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>com.myCompany:mod1</include>
<include>com.myCompany:mod2</include>
<!-- and others... -->
</includes>
<binaries>
<includeDependencies>true</includeDependencies>
<outputDirectory>modules/maven-assembly-plugin</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
(I realize that at some point I will probably need to change <format>dir</format> to <format>jar</format> but for now, I'd just like to get something working)
When I run mvn clean package from the main parent module's directory, I get warnings like this:
[INFO] Reading assembly descriptor: src/assembly/assembly.xml
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'com.myCompany:mod1'
o 'com.myCompany:mod2'
...
Followed by the error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:3.1.0:single (distro-assembly) on project distribution: Failed to create assembly: Error creating assembly archive bin: archive cannot be empty -> [Help 1]
What I actually want:
One jar file containing all of my modules, and all of their dependencies (that is, the *-with-dependencies.jars), as a single jar file with everything contained inside.
I'm really not sure how to achieve this in a multi-module context.
I think I may have resolved it: the child modules mod1, mod2, ... were missing the parent module dependency. I've added
<parent>
<groupId>com.mycompany</groupId>
<artifactId>BigProject</artifactId>
<version>0.0.1</version>
</parent>
to the relevant child modules and now the build doesn't fail, but populates the target directory under the distribution project.
I was upgrading the Spring version of our project and I noticed that the src/ folder doesn't appear as classes when I open the file as an Archive. Instead all I found was an /org folder where I found Spring.
It actually looked like this,
The strange thing was that we got the classes nicely before (as you would expect in JARs) in Spring 1.3.x.RELEASE
My pom.xml looks like this,
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<groupId>spring.mvn</groupId>
<artifactId>dummySpringMvn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<mvn.compiler.version>3.3</mvn.compiler.version>
<spring.framework.version>4.3.8.RELEASE</spring.framework.version>
<start-class>dummySpringMvn.main.Main</start-class>
</properties>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I found my classes in BOOT-INF folder!
This I found thanks for the article https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html.
Furthermore, the project I upgraded was a library to another project. I was able to keep the folder of JARs as conventional creating the JAR without invoking spring-boot-maven-plugin