Couldnt find ab-cd config file for this filepath:config.properties. <phase>package</phase><goals> <goal>shade</goal> </goals> - maven

mvn package - ruunable jar file is unable to locate this properties file. In Eclipse my application runs fine but after i create mvn package. runnable jar file is giving me me null pointer. I used 7zip to view the create runnable jar file i see config.properties is correctly added in jar but my ruunable application jar is not picking it up.
I am getting null pointer here..... Its coming from JAR file which i cant edit.
ClassLoader classLoader = super.getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
if (file.exists()) {
reader = new FileReader(file);
instance.properties.load(reader);
} else {
System.out
.println("Couldnt find ab-cd config file for this filepath:"
+ fileName);
Couldnt find ab-cd config file for this filepath:config.properties Oct 18, 2015 3:47:24 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol java.lang.NullPointerException at java.net.URI$Parser.parse(URI.java:3004) at java.net.URI.(URI.java:577) at java.net.URI.create(URI.java:839) at org.springframework.ws.client.core.WebServiceTemplate$1.getDestination(WebServiceTemplate.java:213) at org.springframework.ws.client.core.WebServiceTemplate.getDefaultUri(WebServiceTemplate.java:188) at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380) at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:372) at
My pom.xml file to build it
<build>
<finalName>AE</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.abcdedf.ghi.main.Start</mainClass>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Related

How to package config.yml and resources in $project--0.0.1-SNAPSHOT.jar

I've a drop wizard project which has the following directory structure:
basedir
pom.xml
config.yml
src
main
resources
myresource.xml
The build portion of
<build>
<finalName>project-${version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>project-package.App</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I run my code from Eclipse, I use command as follows:
$mypackage.App server config.yml
It works fine and I can read the file under resources directory.
When I use mvn to build, in the snapshot jar created, it does not include config.yml and the path to "myresource.xml" is missing even though it is included in the snapshot.jar in the basedir. So, when I run it from my jar file, my code does not find it as resources.
How should I change the pom.xml to include config.yml and package the resource directory such a way that it is accessible as resource.
It is really strange that
this.getClass().getResource("myresource.xml")
does work in IDE but NOT when you run it from a jar.
this.getClass().getResourceAsStream("myresource.xml")
works both inside IDE and from jar.
That resolves the resources issue. However, I need an answer for how to package config.yml file. Should I supply separately in addition to the jar file?

How to use maven-shade and felix to generate a shaded jar with a new manifest

I'm currently working on a project that is attempting to integrate use both shade and felix. The goal is to create a shaded jar that contains only the code we need, then use felix to create the manifest we need. The build part of my pom is as follows:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>${project.build.directory}</manifestLocation>
<niceManifest>true</niceManifest>
</configuration>
</plugin>
</plugins>
</build>
Now the issue I'm running in to is something I've seen elsewhere, but all of those threads seem to die right at this point. So the shaded jar is correctly created, then Felix runs afterwards and puts the MANIFEST.MF file (which is correct as far as I can tell) in target/classes/META-INF/ but it doesn't put that same manifest in the shaded jar. The one inside the jar is the same manifest that existed before Felix ran.
It almost seems like I need Shade to run, then Felix, then re-run the jar creation. Am I missing something?
I'm trying to figure out how to, for lack of a better term, re-package the JAR with the new manifest.
Your main problem is that the manifest must be generated in the jar file, you can generate your Manifest file from the shade plugin (adapt with your needs):
<configuration>
<transformers>
<!-- This bit sets the main class for the executable jar as you otherwise -->
<!-- would with the assembly plugin -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Import-Package>org.apache.common</Import-Package>
<Export-Package>org.test</Export-Package>
<Main-Class>com.br.iacit.tutorialdoJar.ImageLab</Main-Class>
<Specification-Title>Java Advanced Imaging Image I/O Tools</Specification-Title>
<Specification-Version>1.1</Specification-Version>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Title>com.sun.media.imageio</Implementation-Title>
<Implementation-Version>1.1</Implementation-Version>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
</transformer>
<!-- This bit merges the various GeoTools META-INF/services files -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
Output Manifest:
Manifest-Version: 1.0
Toto: test
Export-Package: org.test
Archiver-Version: Plexus Archiver
Built-By: NG673AB
X-Compile-Target-JDK: 1.7
Import-Package: org.apache.common
X-Compile-Source-JDK: 1.7
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_66
Main-Class: tt.tt.main
Edit: I managed to make it so that it compiles correctly, see below:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>META-INF/MANIFEST.MF</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/MANIFEST.MF</resource>
<file>src/main/resources/MANIFEST.MF</file>
</transformer>
</transformers>
<shadedArtifactAttached>false</shadedArtifactAttached>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>src/main/resources/</manifestLocation>
<niceManifest>true</niceManifest>
<instructions>
<Export-Package>test</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

include package in maven while preparing jar

I have below project structure. I wanted to prepare a jar with dependencies including different packages which has .java files.
Project Structure:
src/com/rev/automation/utilities
src/com/rev/automation/testdata
src/com/rev/automation/pages
Main Class:
org.openqa.selenium.remote
How to include "src/com/rev/automation" packages into the jar in maven? i'm preparing the jar using below code, But it is not including packages and files present in "src/com/rev/automation". Kindly suggest
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>org.openqa.selenium.remote.RemoteWElement</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
While using Maven, you need to follow the Maven Standard Directory Structure.
In your case, create a folder structure like src/main/java and put all your code in java directory. So it should now look like src/main/java/com/rev/automation etc.
If you follow this structure, then your package will get included in the jar.
Update:
If you absolutely don't want to / cannot do what is mentioned above, alternatively, you can use the Maven Build Helper plugin. This will tell maven to include the specified directory as an additional source directory.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src</source> // specify your directory here
...
</sources>
</configuration>
</execution>
</executions>
</plugin>

Modifying jar filename using maven

I have a requirement that all the artifacts created by maven bear a build number.
The build number is stored in a properties file. I'm successful with controlling the names of generated EAR and WAR artifacts but not the JAR. Here are the relevant excerpts from pom.xml.
I expected the maven-jar-plugin configuration to work but it does not, I end up with jar always named SelfService-2.jar, whereas when buildNumber.properties contains buildNumber=40, maven generates SelfService-2.40.war and SelfService-2.40.ear.
How do I get the build number into the jar name?
Thanks in advance.
<artifactId>SelfService</artifactId>
<name>SelfService</name>
<packaging>war</packaging>
<version>2</version>
<build>
<finalName>${project.artifactId}-${buildNumber}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<finalName>${project.artifactId}-${buildNumber}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<finalName>${project.artifactId}-${buildNumber}</finalName>
</configuration>
</plugin>
....
I got what I was after by using the following configuration of maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-${buildNumber}</finalName>
</configuration>
</execution>
</executions>
<configuration>
<finalName>${project.artifactId}-${buildNumber}</finalName>
</configuration>
</plugin>

Maven deploy + source classifiers

I'm trying to deploy a Maven artifact with a classifier. Since I need both the sources and the JAR (I'm using it from GWT), I would like to get artifact-version-classifier.jar and artifact-version-classifier-sources.jar. However, it works fine with the compiled JAR, but fails with the sources (the output sources JAR has a wrong name).
This is the configuration that I have so far:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classifier>prod</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.build.finalName}-prod</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<classifier>prod</classifier>
</configuration>
</plugin>
And this is the output I'm getting for mvn deploy:
Uploading: http://juicebox:8080/archiva/repository/snapshots//ar/com/nubing/afip-connector/1.0-SNAPSHOT/afip-connector-1.0-SNAPSHOT-prod.jar
237K uploaded (afip-connector-1.0-SNAPSHOT-prod.jar)
But this one has a wrong name:
Uploading: http://juicebox:8080/archiva/repository/snapshots//ar/com/nubing/afip-connector/1.0-SNAPSHOT/afip-connector-1.0-SNAPSHOT-sources.jar
228K uploaded (afip-connector-1.0-SNAPSHOT-sources.jar)
Sadly, attaching a source JAR with an arbitrary classifier is not supported by the source plugin. When the source artifact is attached, the classifier is hardcoded (as of version 2.1.2 of source plugin).
You can work around the issue by getting the source plugin to generate the JAR but not attach, and attach it with the build helper plugin's attach artifact goal.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-source-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.build.finalName}-prod-sources.jar</file>
<type>jar</type>
<classifier>prod-sources</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
Used the same workaround as prunge for this. But that's no longer necessary. This is a reported Bug that was fixed in version 2.2 in June 2012: Just set the property <classifier>. Tested with 2.2.1 .
A bit more of an updated answer, using sources and javadoc
<maven.javadoc.version>3.0.1</maven.javadoc.version>
<maven.source.version>3.0.1</maven.source.version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.version}</version>
<configuration>
<classifier>jre10-sources</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.version}</version>
<configuration>
<classifier>jre10-javadoc</classifier>
</configuration>
</plugin>

Resources