Add Property File from Another Jar (Maven) - java

I am trying to do a maven build (lets say A.jar), where the property file resides in one of the dependencies (lets say, B.jar) that I am adding. Is there any way to add the properties from B.jar to A.jar or to read them in some way from B.jar? I looked at some questions, but none of them were helpful. I need to run an exec-maven-plugin, the class needs to read two property files from B.jar to run. At this point, I am moving around in circles. Is there something like an overlay (maven-war-plugin) in maven-jar-plugin or something that is similar?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<archive>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<classifier>classes</classifier>
<forceCreation>true</forceCreation>
</configuration>
</plugin>
This is my plugin configuration at the moment. But the plugin is very short of options! Thanks in advance!

Related

No main manifest attribute, but I have it in my jar

I am trying to run my java web app on AWS EC2 instance. The error I am getting is
no main manifest attribute, in app.jar
Going through other questions, I found that the problem can be with MANIFEST.mf file. I have that file under META-INF folder, however its missing Main Class tag.
I developed Java web app using servlets and stuff, but I didn't use SpringBoot, and I am unsure what the starting point of my program is.
Here is a part of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
I thought that maven-jar-plugin would solve this by itself, but unfortunately that is not the case.
As a side note, I tried both war and jar packaging.
Any ideas how to make this work? Should I hardcode something in MANIFEST.mf by myself?
My goal is to start my webapp on EC2 using this command:
java -jar app.jar
for the maven-jar-plugin there is a place to configure the manifest. I have it like this in my project
my project is years old so please have a look at the documentation for the plugin: https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>webServer.main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

No suitable driver found for jdbc:postgresql://localhost:5432/gis

I've spent the last 3 hours trying to get my Java program to interface with my Postgres server. I cannot get past the error message "No suitable driver found for jdbc:postgresql://localhost:5432/gis". It is a Bukkit plugin, and I am using IntelliJ IDEA.
The code:
try
{
//Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
catch(Exception e)
{
getLogger().info(e.getMessage());
}
Things I have tried:
java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar
adding the jdbc file internals directly to my jar file
adding the jdbc file as a dependency within the IntelliJ project
switching to maven, and putting the following in pom.xml:
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
I am unable to get past the error I posted. At this point, it has taken over my entire evening.
I've stumbled with this issue several times when developing Bukkit/Spigot plugins that make use of MySQL databases. The process for Postgres should be the same.
Usually, this error happens when your plugin can't find the PostgresqlJDBC driver. You have two workarounds:
Option 1. Adding the .jar to the plugin's classpath:
It's recommended that you set the libraries inside plugins/lib as then your server won't try to load the libraries as a Bukkit plugin. Add the prefix lib/ to your classpath by adding this configuration in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <-- don't know if this is needed -->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Then make sure to put your postgresjdbc.jar inside a folder called lib inside your plugin's folder.
Option 2. Add dependencies directly in your jar:
Note that this option will increase your plugin's jar size.
This can be done via Maven's assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
If you open your plugin's jar with a file compressor like 7-zip you should there should the driver's classes in it apart from your plugin ones.
Let me know if this solved your issue.

How to refer maven dependency jars from an executable jar?

I have an executable jar which needs to be placed in an Eclipse Maven project (Lets assume in the first folder of the project). In the manifest file of this jar, I need to refer to the maven dependency jars. How can I specify that in MANIFEST.MF file using pom.xml? Is it possible?
Yes, you can specify jars using maven-jar-plugin.
you can specify dependent jars in manifest tag
E.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

How to set current Project version and Project build timstamp in a interface by using Maven?

I have a question concerning Maven. I have created a Jar file the name "MyFinalFarFileName_1.0.0-SNAPHOT_20140730.jar". Here is a small code snippet from my pom.xml file:
<groupId>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
</properties>
<build>
<finalName>MyFinalJarFile_${project.version}_${timestamp}</finalName>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifestEntries>
<source-jdk>1.6</source-jdk>
<target-jdk>1.6</target-jdk>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>myMainClass</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>assembly-jar-Id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
Currently I´m set a manual build timestamp and project version number in a Java Interface like this:
public MyInterface {
public String versionNumber = "My Project Version: 1.0.0-SNAPSHOT";
public String versionBuildTimeStamp = "My Project BuildTimeStamp: 20140730 11:34";
}
Is it possible to set these kind of information by using maven? Or it is necessary to create a propertie file with these kind of information by using maven and combine this popertie file with my Java Interface?
It is not enough to set this information in the manifest file of my created Jar file.
How can I solve this problem?
It is possible however not at all suitable, better to write a maven plugin that will write these properties to a properties file and then you can fill in your constant values reading that properties file
There are tons of ways to do this, take a look on the Maven release plugin.
The easiest way in your specific case could be to read a property file that is updated by the Maven resources plugin with the current version number and timestamp (it can process files and replace variables easily).
Maybe the Build Number Maven Plugin can help you.

Missing Main-Class attibute in MANIFEST.MF in Maven generated jar file

I am currently experimenting with Maven in Eclipse (m2e-Plugin) and tried to build and run the Hello World example project. However, when launching the generated jar, nothing happens. I checked the MANIFEST.MF and noticed that the Main-Class attribute was missing. After adding the attribute, the jar could be launched.
Why does Maven not add this attribute?
Have a look at this link:
https://maven.apache.org/shared/maven-archiver/examples/classpath.html#aAdd
You can find there how to configure your maven project to run specific class. You have to add maven-jar-plugin configuration with mainClass defined.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>

Categories