Assign all classes in a package as a maven variable - java

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?

Related

My java application does not read my files (maven project)

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.

Maven Resource Custom File Path

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.

Different property files when depend on a library

I have two different projects, A and B. Both projects load their own properties with:
new PropertiesConfiguration("application.properties")
Each application.properties is located in the Maven resource root directory.
When I execute Project B alone, all properties are loaded correctly.
Project A depends on Project B. When I execute Project A, the property class of Project B cannot load the correct properties, because the properties of Project A are loaded.
How can each Project load the correct properties even if they are included as a dependency. I have to specify to load the resources in different contexts, but I don't know how.
My maven settings for resources:
<resource>
<directory>${project.build.sourceDirectory}</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
You probably have some specific packages for Project B like
com.company.packageB
So put your application.properties in
src/main/resources/com/company/packageB/application.properties
And same for the Project A
You can iterate over all application.properties files like this
for (Enumeration<URL> urls = ClassLoader.getSystemResources("application.properties"); urls.hasMoreElements();) {
URL url = urls.nextElement();
}
Depending on your application you have to make a choice how to proceed if multiple property files are found. I hope this helps.

Eclipse: project version as a parameter in multiple files

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).

Append behavior to "build" action on NetBeans

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.

Categories

Resources