Embedding Maven project information into Swing Application Framework resources?
I have a Maven project using Swing Application Framework and would like to add project information from pom.xml to my global application resources to avoid duplication.
The base application (provided via netbeans) uses Application.title, Application.version, Application.vendor, Application.description Resources, etc. for windows headers and box config but i can't find a way to set these values programmatically and i am not maven maven so i don't have the skills to inject them during build.
Anyone have any recommendations on how best to achieve the desired result?
a source to share
You can try using filtered resources . If you create a properties file, let's say src/main/resources/com/myapp/app.properties
that looks like this:
version=${project.version}
name=${project.name}
id=${project.artifactId}
They need to enable filtering in pom.xml
:
<build>
<resources>
<resource>src/main/resources</resource>
<filtering>true</filtering>
</resources>
</build>
Now when maven builds your project, it will expand the properties file and place it on the classpath. Then you can simply call getResourceAsStream ("/com/myapp/app.properties") to read it in your application.
Whist maven creates the file automatically /META-INF/maven/$groupId/$artifactId/pom.properties
, it may not have all the information it needs.
a source to share
Another way would be to create a separate properties file using property-maven-plugin and then add this properties file to the application package names:
To pom.xml
write a file application.properties
:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>${project.build.outputDirectory}/application.properties</outputFile>
</configuration>
</plugin>
...
</plugins>
...
</build>
To be included application.properties
in your application:
public class MyApplication extends SingleFrameApplication
public MyApplication() {
super();
addGeneratedApplicationProperties();
}
private void addGeneratedApplicationProperties() {
ResourceManager resourceManager = getContext().getResourceManager();
getContext().setApplicationClass(MyApplication.class);
List<String> bundleNames = new LinkedList<String>(resourceManager.getApplicationBundleNames());
bundleNames.add(0, "application");
resourceManager.setApplicationBundleNames(bundleNames);
}
...
}
However, I believe the maven-filter-solution is more elegant.
a source to share