ImplementedVersion is not accessible in exec:java target - java

I have used this how2 to add the Version number to our produkt: http://www.christophkiehl.com/easy-way-to-display-your-apps-version-using-maven-and-manifest
when i now build a jar with mvn assembly:single i see the correct version.
But when i just run everything with mvn exec:java i get null...
what do i have to do that App.class.getPackage().getImplementationVersion() does not return null when i just start the programm from the non-jar?

The technique that you have used to add version number works only when built as a jar.
A different way to achieve the same would be to have a properties file which gets updated with the project version during build, which in turn is read by your java code.
Say, you have a file version.properties in src/main/resources, which has an entry
product.version=${project.version}
In the place you call App.class.getPackage().getImplementationVersion(), you read this property and display the contents.
This will work in both jar and non-jar case.
Update: You would need to update the pom to enable filtering for resources - essentially add a snippet like the below (refer this for details).
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

Related

How to call a SO native library while package with maven?

I have developed a project with swing, maven with some native libraries. Now i have an issue with calling SO from java after generating jar from maven package. I included so in POM.xml. And it included that file inside jar.But it wont link both.
I had an error while executing jar like
" Exception in thread "main" java.lang.UnsatisfiedLinkError: no projectso in java.library.path"
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/lib</directory>
<includes>
<include>my.so</include>
<include>cv2.so</include>
</includes>
</resource>
</resources>
And also want to add library folder and export it into jar.Is there any way to do it?
Your native library path is not correct for your program to run. see Call c function from Java
use -Djava.library.path=/path/to/libs to set the path before executing the program.
use System.loadLibrary("HelloWorld") to set the path in runtime
please take look at the link I post again.

How to create custom files from tests in Maven

I have a tests writen in TestNG and running through Maven Surefire. In the tests there are lot of database updates and inserts and I want to create a report of which inserts or updates couldn't be performed (because of invalid data, or whatever). For that I would like to create a CSV file where I will track the IDs and exception messages. I want this file to be created in the target directory of my project (and all of the subprojects). How can I obtain the configuration from maven and use the directory to have all things together? I don't want to hardcode the path to file.
Thank you,
Filip
You have to think the other way around: which information should Maven push to/for my test (instead of: how can I pull information from Maven).
One option: create a test.properties under src/test/resources and add outputDirectory = ${project.build.testOutputDirectory} to this file.
In your pom.xml specify
<build><testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<testResource>
...etc
Now you can read this file from the classpath and use it in your tests.

How to make Maven copy resource file into WEB-INF/lib directory?

I'm using NewRelic for monitoring. I want Maven to package both newrelic.jar and newrelic.yaml files into my WEB-INF/lib inside the war file. With the newrelic.jar there is no problem since it's a simple dependency, but newrelic.yaml is a resource file. It resides in resources directory. I want Maven (war plugin) to copy it to WEB-INF/lib when packaging the war.
Thanks.
Alex
While I agree with #matt b that this is odd, here's what you can do:
Try changing the configuration of the maven war plugin to include a webResource:
<configuration>
<webResources>
<resource>
<directory>pathtoyaml</directory>
<includes>
<include>**/*.yaml</include>
</includes>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
The directory is relative to the pom.xml. See the plugin's documentation for more info.
You can also specify most configuration for the New Relic agent including the location of the config file via flags passed to the java command. In this case, it's something like:
-Dnewrelic.config.file=/etc/newrelic.yml
(ok, it's exactly like that, but you need to specify whatever path you need if it's not that.)
You should try adding .yaml file to newrelic.jar's resources, later
you can access it via classpath.
Or try changing/overriding build.xml build target by adding something like < copy file=".yaml"
todir="WEB-INF/lib" />
Try googling for more info on changing build.xml.

Can resource directory be a subdirectory of sources in maven?

I am trying to create a maven build for one of our legacy projects. Since one of the requirements is to be still compatible with the legacy ant build script I can't change the project directory structure.
The problem is the current directory structure which is as follows:
+ src
+ java
+ com
+ whatever
+ whatever2
+ resources (!)
My goal is to have source directory src/java and resource directory src/java/com/whatever/whatever2/resources.
Obviously I need to set <sourceDirectory>src/java</sourceDirectory>. This is fine.
But I would also need to make the resources maven resource directory. Trying the following:
<resources>
<resource>
<directory>src/java/com/whatever/whatever2/resources</directory>
</resource>
</resources>
But once I do this and run mvn clean package it gives me:
[INFO] No sources to compile
Once I remove the <resources> section the module is compiled just fine and have all the classes inside. Any tips on how to solve this? Thanks
We have a similar setup (XMBean descriptors next to the MBean implementations using them), but exclude java files from the resources:
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>

Including .properties files with .class in JAR after an mvn install

Imagine I have a project I want to build using maven. My project doesn't, unfortunately, respect the maven default layout. So I'm having two source folders A & B containing .properties files with .java sources.
After an mvn install, my .properties files are not packaged in the jar with my .class generated files. Is there a way to do this automatically, you'd probably propose to use <resources> tag to solve this kind of problems; this obviously works I know, but i'll have to each time specify where my .properties files are stored, and where I want them to be packaged in the JAR, this is not a solution for me since I have multiple source folders (hundreds) regularly updated.
<resource>
<targetPath>com\vermeg\jar2</targetPath>
<filtering>true</filtering>
<directory>${basedir}/a/jar2</directory>
<includes>
<include>*.properties</include>
</includes>
<excludes>
<exclude>*.java</exclude>
</excludes>
</resource>
Will I have, for each source folder, to write this in my POM, does anyone knows a simpler automatic way to do this ?
Regards,
Generally you can use Maven properties and profiles to solve this kind of problem.
For example edit your pom to:
<resource>
<targetPath>${myresources.targetpath}</targetPath>
<directory>${myresources.directory}</directory>
...
</resource>
and define myresources.directory and myresources.targetpath in command line using -Dname=value or in conditional profile or by using other properties available in your build.
If you explain your project structure and/or conditions and their relation to your variable (targetPath and directory) I may be able to help more with your question.
Specify it once in a parent pom instead, and all the children should inherit the same setup...
Would it be impossible to change to using the standard Maven layout, with all your properties in a parallel 'package' hierarchy under src/main/resources? Then you don't need to specify anything; all .properties files will be packaged.
If you do this, you'll need to enable filtering as it's off by default. You may have to explicitly declare the resource directories again, as when you declare them, this seems to override the ones you get 'for free'.
As for your multiple source folders, a multi-module Maven project would probably be the best fit, with A and B being children of some new parent project.
This might seem like quite a lot of work, but Maven's conventions are fairly reasonable, and it tends to be a painful experience if you go against them.

Categories