Bundle local JAR dependency within the target JAR - java

My project structure is something like this.
ProjectX - depends on ProjectY which is a local JAR, added as a dependency like this:
<dependency>
<groupId>com.wow.projecty</groupId>
<artifactId>projecty</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>/Users/somepath/ProjectY.jar</systemPath>
</dependency>
Now, I'm creating a JAR for ProjectX with all the dependencies bundled in the JAR using this.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>ProjectXDriver</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This is bundling all the dependencies from Maven but not the ones from local filesystem. In this case, classes from ProjectY are missing from the final JAR. (Also checked using jar tf)
What am I missing?

Quick fix I found for this. Just install the JAR in the local maven repository and use it normally (without system scope / systemPath.
mvn install:install-file -Dfile=ProjectY.jar -DpomFile=../pom.xml

Related

No suitable driver found for jdbc:postgresql - executable java jar

When I run the project as a java application in eclipse it works perfectly but running a java -jar on windows cmd throws the SQL exception (No suitable driver found)
I have used maven(maven-assembly) to create the executable jar, a jar-with-dependencies is created in project\target.
The postgresql jar (postgresql-42.2.5.jar) exists in the project\lib folder.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.project.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
After running mvn clean package :
A manifest file (project\src\main\resources\MANIFEST.mf) is copied into project\target\classes folder. The manifest file contains Class-Path: lib/postgresql-42.2.5.jar.
I have added the dependency in my pom.xml as well.
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.driver.version}</version>
</dependency>
</dependencies>
I was hoping that the Class-Path in manifest file would take care of this issue but it did not.
Might be a duplicate post but none of the existing solutions fixed my problem.

How to have a final jar with the dependency inside that jar in the jar format?

I need a configuration for Maven where all the libraries inside the project are in the final jar in the jar format... So i need to have jars inside the final jar. For that i can only use maven. I already tried without success with plugins like one-jar.
Thanks
To make a fat jar that includes all you jar files, add the following code to your pom.xml. When you clean and build the project this will automatically make a fat jar file. You need to give your main class inside <mainClass> tag. That's it.
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>final-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Not sure for nested jar because I haven't tried yet.
But I have created .jar file with all project dependencies(specified in pom file)
Try following mvn command in terminal...
mvn org.apache.maven.plugins:maven-assembly-plugin:2.6:assembly -DdescriptorId=jar-with-dependencies package

log4j class not found in generated jar

I generate jar file with maven-assembly-plugin plugin. I use java -jar to execute jar. I got error message:
log4j: WARN JmDNS or serviceInfo not found
I tried to use path to jar in -classpath, but got same error.
Plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>test.LeanFTest</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}-fatjar-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/leanft-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
Most likely, this boils down to:
Your maven build does not include dependent artefacts into your JAR file. In other words: the JAR you create does not include the logj4 JARs. You can change that with your maven config, see here for details.
As your JAR doesn't contain the dependencies, all JARs you depend on must be in your classpath. Meaning: when you run your new JAR on the command line, all elements that might be required for running it must be present on the classpath.

run maven java project using batch file

I've a java application in which I'm using maven to load dependencies. My main method is in App.java and there are various other classes. This is a spring based application.
I've to run this application using batch file.
This is what I've tried so far:
made a manifest file to give main class name
generated a jar of application
in a lib folder placed all the jars which my app uses
in manifest gave all the jars path
But I want to know if there is any other way I can achieve the same thing. Here in manifest I've to give all the jars names
Also in application jar, a manifest file is automatically created. So I've to manually edit it to give main class name and all dependent jars.
Any help appreciated.
Thanks.
Use the Maven Jar Plugin to do what you want. You can configure it to place all your manifest entries to meet your needs.
<plugin>
<!-- jar plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Main-Class>package.path.for.App</Main-Class>
<implementation-version>1.0</implementation-version>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix> <!-- use this to specify a classpath prefix, in your case, lib -->
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
To facilitate copying all your dependencies to a particular folder, use the Maven Dependency Plugin:
<plugin>
<!-- copy all dependencies of your app to target folder-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory> <!-- use this field to specify where all your dependencies go -->
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>

How do I create a Netbeans style Jar with all dependencies in a lib folder?

As the question says, how to package a Netbeans Maven project exactly the way a Netbeans native project is packaged:
All the dependencies in a separate lib folder
The main project jar with a manifest that includes the lib folder on it's classpath
In your pom.xml file ...
1) Add this code to your project->properties node. This will define your main class in a central place for use in many plugins.
<properties>
<mainClass>project.Main.class</mainClass>
</properties>
2) Add this code to your project->build->plugins node. It will collect all your jar dependencies into a lib folder AND compile your main class jar with the proper classpath reference:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Categories