将Maven项目信息注入Swing应用程序框架资源中?
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用过滤资源 。 如果您创建一个属性文件,请说
src/main/resources/com/myapp/app.properties
如下所示:您需要在
pom.xml
中启用过滤>:现在当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:Them you need to enable filtering in your
pom.xml
: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.您可以将它们保存在单独的属性文件中,并从 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.
我会尝试使用 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.
另一种方法是使用properties-maven-plugin生成一个单独的属性文件,然后将此属性文件添加到应用程序包名称中:
对于
pom.xml
写入application.properties< /code> 文件:
用于将
application.properties
包含到您的应用程序中:但是,我发现 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 writeapplication.properties
file:For including the
application.properties
into your application:However, I find the maven-filter-solution way more elegant.