I’m working on a netbeans platform/maven application, that requires a custom .jar to be on its /platform/core folder when its run.
We use IzPack to create the installer, so it’s easy to copy the .jar to its destination when the app is installed.
However, on the development environment, how can I copy the required .jar file to the /target/myApp/platform/core folder every time the “Build”, “Clean and build” of “Build with Dependencies” action is executed in NetBeans?
I’ve completely no idea of where to start or what to do, so any pointers to reading material are greatly appreciated.
There is few ways to do it, but I think the best way is adding new resource description in your pom.xml. Let your jar be placed at ‘project_folder/jar-resources’ folder then your pom.xml will look like:
<build>
...
<resources>
...
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>jar-resources</directory>
<targetPath>../myApp/platform/core</targetPath>
</resource>
</resources>
...
</build>
Remember that if you don’t use standard resource place ‘src/main/resource’ you can’t use first resource declaration.
Related
I am using a Java ServiceLoader to dynamically load all the classes which are extended from an Abstract Class (AbstractDeathHandler). Currently, every time I create a new implementation I have to add the reference into my resources/META-INF/services/... file. After much research I was unable to come up with any real solutions. It would be very useful if it were possible to set a Maven variable that could generate this list while compiling.
Relevant pom.xml:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
This is the file I want to be able to use a Maven variable with resources/META-INF/services/com.jordanschnur.deathmessagesplus.handlers.AbstractDeathHandler:
com.jordanschnur.deathmessagesplus.handlers.EntityDeathExplosionHandler
com.jordanschnur.deathmessagesplus.handlers.EntityAttackEntityHandler
com.jordanschnur.deathmessagesplus.handlers.FallHandler
Is it possible to define something like ${project.handlers} as all the files in this package?
I'm using the Unix terminal to build Java Projects. And I have created few Shell Script(.sh) files to execute the Jar files. So those sh files contain the version name of the jar file.
So I come across on a new requirement that when I upgrade my version Of the POM by executing a maven command and simultaneously I want to change the sh file's content according to that.
As an example
content of
A.sh
#!/bin/bash
"-jar X.3.16.0.jar"
When I execute the command like
mvn versions:set -DnewVersion= X.3.17.0-SNAPSHOT
I want to change the content of A.sh to be like
#!/bin/bash
"-jar X.3.17.0-SNAPSHOT.jar"
I used to do this change manually. But in the long run it won't be a good idea. So I thought to solve this by two approaches.
Method 1
Implement a mechanism to listen to above mentioned maven command and find the specified text in the sh file and replace it.
Method 2
Implement a new Shell Script with the maven command included and find and replace the text in A.sh file. This newly implemented sh file will have an argument to take the new version.
Question:
What will be the best approach to solve this problem? If none of the above will do it please help me out.
Thank you!
You could use the filter feature of the Maven resources plugin. Usually the resources plugin simply copies files from the resources directory (default src/main/resources) to the output directory (default target/classes) and includes the files into the jar (or whatever packaging you are using). Filtering enhances this process by resolving variables within the copied files while writing them to the target directory. Variables could be all standard Maven properties or any self-defined value that is set in the pom. The current version of the project is referenced with ${project.version}.
Filtering is enabled by declaring a resource directory to be filtered. The easiest case would be to filter all resources:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Another option is to have a separate directory for resources that should be filtered:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/filtered_resources</directory>
<filtering>true</filtering>
</resource>
</resources>
If src/main/filtered_resources contains a file with the text
echo Version: ${project.version}
the result would be after the build
echo Version: 0.0.1-SNAPSHOT
If the filtered scripts should not be copied to the target directory but to a separate directory, the copy-resources goal of the resources plugin is useful.
I have an application in a Java simple project. However, I need to paste this project to a Maven project. So, I basically made a simple Maven project, and I copied and pasted all my classes into it. I need a war to run in a server, and I need to run a Main like a Java application, because this application configures the war application. However, when I run Main, I get some errors that I wasn't having before:
java.io.FileNotFoundException: resources\config.properties (The system cannot find the path specified)
when in the code is:
input = new FileInputStream("resources/config.properties");
This didn't work either:
faceDetector = new CascadeClassifierDetector("D:/retinoblastoma/workspace/Resources/CascadeClassifiers/FaceDetection/haarcascade_frontalface_alt.xml");
How can I fix this?
In a simple Mavne project, all the resources should be located in src/main/resources.You can get the properties file then by (for a non-static method) :
Properties prop = new Properties();
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
For a static method, use :
<Class name>.class.getClassLoader().getResourceAsStream("config.properties");
Refer this link for more information :
Reading properties file
If you're using a "simple maven project", the maven way is for there to be a src/main/resources folder. Did you configure your pom.xml to do something different than that?
If you've done the jar creation portion correctly, the right way to get a file that's on the classpath is:
getClass().getResourceAsStream("/path/to/resource.ext");
The leading forward slash is important!
If the file is NOT on your classpath (in other words, this will be the case if the above doesn't work), you probably need to configure maven to use a different resources directory.
You do that like this (change the arguments as appropriate):
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
Then, your file will be on your classpath and the above code will work. Read the linked documentation above for more information.
Good time!
I have such a pom snippet:
<build>
<resources>
<resource>
<directory>${project.build.directory}/additional-resources</directory>
<targetPath>targetDir</targetPath>
<includes>
<include>copied/bindings/**/*</include>
</includes>
</resource>
</resources>
...
</build>
After a compilation phase, I see such a hierarchy in the build directory: targetDir/copied/bindings/test/... which fully copies the path in the generated-sources directory.
Is there a way to tell Maven to make the build/targetDir/test/... (trim the path) directory?
As far as I know, the Maven resources plugin does not provide a way to trim the path like you need. I can think of two ways to solve this.
Change whatever is generating the resources so they are in the final directory structure required (e.g. ${project.build.directory}/additional-resources/test/...) . That way when the resources plugin copies the structure, it will be what you want.
Use ant, via the maven-antrun-plugin. Ant has a concept of a "flatten mapper" which allows you to configure the target directory structure during the copy.
I am developing a project which uses versioning:
version name is typed in POM file
It is also used as a part of to-be-created .msi file name : file_[version].msi
It is used in a service name, after this project is installed from .msi
Those params are kept in locations as follows:
a).properties file as a Spring param: version=0340
b)in pom.xml <package-version>0340</package-version>
c)as a <filename><version>.wxs file, used by build.xml
d) also in the abovementioned .wxs file as a MsiProductVersion = "3.4.0" (notice the dots)
Is there a way to define a parameter in some other config file, that would populate those files with proper data, as to keep the version in one place only. Now it is easy to overlook one param, and build a 340.msi which will display 330 Service as its name. I find it difficult since not all files belong to one model (like Spring).
You can configure Maven to replace "variables" in resources. Add this to your POM:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Now you can use ${project.version} and it will be replaced with the version from the POM in all files below src/main/resources and src/test/resources.
As for the other files, you can either use an embedded Ant task in your POM or write a small script (Ant, bash, whatever you like) that reads the POM and creates the other three files from templates.
Another option is to write a unit tests which reads all files and makes sure that they contains the correct values. That way, the version won't be updated automatically but a) the version doesn't change that often (which probably causes your problem) and b) the tests are much more powerful than what you can do in a script (they can, for example, read&update binary files).