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?

+1


a source to share


4 answers


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.

+4


a source


You can save them in a separte properties file and read it from both pom.xml and your application.

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



I would choose the first option.

+1


a source


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

0


a source


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.

0


a source







All Articles