将Maven项目信息注入Swing应用程序框架资源中?

发布于 2024-07-20 05:29:57 字数 286 浏览 5 评论 0原文

我有一个使用 Swing 应用程序框架的 Maven 项目,并且希望将 pom.xml 中的项目信息注入到我的应用程序的全局资源中以避免重复。

基本应用程序(通过 netbeans 提供)使用 Application.title、Application.version、Application.vendor、Application.description 资源等作为窗口标题和框配置,但我找不到在运行时以编程方式设置这些值的方法,我不是专家,所以没有在构建时注入它们的技能。

有人对如何最好地达到预期结果有任何建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人生百味 2024-07-27 05:29:57

您可以尝试使用过滤资源 。 如果您创建一个属性文件,请说 src/main/resources/com/myapp/app.properties 如下所示:

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

您需要在 pom.xml 中启用过滤>:

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

现在当maven构建你的项目时,它会扩展属性文件,并将其放置在类路径上。 然后你可以调用 getResourceAsStream("/com/myapp/app.properties") 将其读入您的应用程序。

虽然 maven 会自动创建一个文件 /META-INF/maven/$groupId/$artifactId/pom.properties,但该文件可能没有您需要的所有信息。

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.

风筝在阴天搁浅。 2024-07-27 05:29:57

您可以将它们保存在单独的属性文件中,并从 pom.xml 和您的应用程序中读取它。

另一种选择是从类路径读取 pom.xml 文件(mvn 会将其放入 META-INF 文件夹中)并将其解析为纯 xml 文件。

我会选择第一个选择。

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.

愛放△進行李 2024-07-27 05:29:57

我会尝试使用 maven-antrun-plugin。 将必要的 maven 属性传递给 ant 并创建一个 ant 构建脚本,该脚本直接修改应用程序属性文件或 spring 上下文配置。

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.

丑丑阿 2024-07-27 05:29:57

另一种方法是使用properties-maven-plugin生成一个单独的属性文件,然后将此属性文件添加到应用程序包名称中:

对于pom.xml写入application.properties< /code> 文件:

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

用于将 application.properties 包含到您的应用程序中:

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);
    }

    ...
}

但是,我发现 maven-filter-solution 方式更优雅。

Another way would be to generate a separate properties file with the properties-maven-plugin and then add this properties file to the application bundle names:

For the pom.xml to write application.properties file:

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

For including the application.properties into 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 find the maven-filter-solution way more elegant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文