views:

488

answers:

3

I have a Maven project using the Swing Application Framework and would like to inject project information from the pom.xml into my application's global resources to avoid duplication.

The base application (provided via netbeans) uses Application.title, Application.version, Application.vendor, Application.description resources etc for Window titles and about box configuration but I can't find a way to set these values programatically at run time and I'm not a maven maven so don't have the skills to inject them at build time.

Anyone have any recommendations on how best to achieve the desired result?

+1  A: 

You can keep those in separte property file and read it from both pom.xml and your application.

Another option is to read pom.xml file from classpath (mvn will put it in META-INF folder) and parse it from there as plain xml file.

I would go with first option.

Dev er dev
+1 for first solution
rudolfson
Neither of these options address getting the information into the resources as expected by the Swing Application Framework though.
Rob Oxspring
Or are you suggesting that I reference the SAF properties to populate the maven pom? How would I go about that?
Rob Oxspring
@Rob That's how I understood it. Have one property file with application name etc. and let Maven and Spring load it.
rudolfson
A: 

I would try using the maven-antrun-plugin. Pass the necessary maven properties to ant and create an ant build script which modifies an application properties file or the spring context configuration directly.

rudolfson
+1  A: 

You could try using filtered resources. If you create a property file, say src/main/resources/com/myapp/app.properties that looks like this:

version=${project.version}
name=${project.name}
id=${project.artifactId}

Them you need to enable filtering in your pom.xml:

<build>
  <resources>
    <resource>src/main/resources</resource>
    <filtering>true</filtering>
  </resources>
</build>

Now when maven builds your project, it'll expand the property file, and place it on the classpath. Then you can just call getResourceAsStream("/com/myapp/app.properties") to read it into your app.

Whist maven does automatically create a file /META-INF/maven/$groupId/$artifactId/pom.properties, this may not have all the information you need.

Dominic Mitchell