Maven资源过滤

2024-02-29

我想将构建信息写入属性文件。我找到了 Maven 资源过滤插件。这就是我的 pom 相关部分的样子:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>project.mygroup</groupId>
        <artifactId>prject</artifactId>
        <version>1.0-20190130-1</version>
    </parent>

    <artifactId>project-war</artifactId>
    <packaging>war</packaging>
    <version>1.0-20190130-1</version>

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>
        yyy-MM-dd HH:mm
        </maven.build.timestamp.format>

    </properties>

    <dependencies>
    ...
    </dependencies>

    <build>
        <finalName>project</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <!-- Create war from the project -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>

            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>    

</project>

如果开始mvn clean package构建成功了,但是我的build.propertiessrc/main/resources 下的文件将不包含构建信息。

我的属性文件如下所示:

build.version=${pom.version}
build.date=${timestamp}
build.url=${pom.url}
build.name=${pom.name}

我究竟做错了什么?谢谢你!


扩展评论/简短回答:

你必须看看target/classes...not at src/main/resources!;)

...文件在src/main/resources保持未被过滤/触及。


哦,好棒。抱歉,我可以在 target/classes 文件夹中找到属性文件。但如何从我的应用程序中读取此属性文件?

请另请参阅此处:-在基于 servlet 的应用程序中,配置资源文件应该放在哪里以及如何读取? https://stackoverflow.com/q/2161054/592355

..使用“标准”java:

// assuming src/main/resources/build.properties
Properties buildProps = new Properties();
buildProps.load(
    //this is fail safe for most situations (test/web container/...), but *any classloader* could do it.
            Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("build.properties")
);
String buildDate = buildProps.getProperty("build.date");

..与(例如)弹簧:

@PropertySource("classpath:/build.properties")
...
@Value("${build.date}")
String buildDate;

But,自从你标记了java-ee /questions/tagged/java-ee(,您应该有一种非常具体且“复杂”的方法来执行此操作(将属性加载到应用程序中)),所以我们应该问! :) (看:http://www.adam-bien.com/roller/abien/entry/injecting_properties_into_java_ee http://www.adam-bien.com/roller/abien/entry/injecting_properties_into_java_ee)


谢谢,现在可以了,但是构建时间戳看起来不像这样的格式: yyyy-MM-dd HH:mm 我的格式如下: 1549362759203你有什么主意吗?

嗯,到目前为止,没有任何线索......对我来说,它按预期工作,生成:

build.date=2019-02-05 10:55

..以及(已编辑)Properties.load() code.

(也许你“覆盖”timestamp属性......这听起来不像是一个非常好的(个人)属性名称(因为any1/thing(可以)引用“时间戳”,更好的是像com.my.company.myVerySpecialTimestamp!?;) 以及:看看target/classes/build.properties告诉你出了什么问题:

  • Maven资源过滤
  • 或属性加载(在您的代码中)

  • https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
  • https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html
  • ... https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Maven资源过滤 的相关文章

随机推荐

  • 如何在 Objective C 中对 NSMutableArray 进行排序 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在开发计时器应用程序 我有一个数
  • 正则表达式用 HTML 标签包裹字符串

    我有一个文本区域 其中有很多行 如下所示 1 stuff 2 more stuff 123 even more stuff 我正在使用正则表达式来查找 num 模式 0 9 我想让它们成为锚标记 例如 a href 123 123 a 但它
  • 如何在数据库中插入时间戳? [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我收到此错误 java lang IllegalArgumentException 时间戳格式必须为 yyyy mm dd 时 分
  • 两个 pandas 数据帧之间的欧几里德距离

    我有两个数据框 df1形式的 user id x coord y coord 214 55 2 22 1 214 55 2 22 1 214 55 2 22 1 and df2 形式相同 但用户不同 user id x coord y co
  • 无法安装mbstring php冲突

    我正在尝试使用 php 5 6 安装 php mbstring 并收到此错误 Error php56w common conflicts with php common 5 4 45 3 el6 remi x86 64 You could
  • 当 JavaFX 中选定的选项卡更改时如何聚焦特定节点?

    我想将焦点设置到选项卡内容中的特定节点 我添加了一个变化监听器 to 选定的项目属性如下 假设该类包含一个名为第二个节点类型的Node tabPane getSelectionModel selectedItemProperty addLi
  • html5数据属性不区分大小写吗?

    我最近注意到chrome转换数据属性data 为小写 e g div div 将被转换为 div div 在 Chrome 开发工具中 当我用 javascript 调用该属性时 console log e hasAttribute dat
  • Jekyll 如何处理 _posts/subdir 中的帖子

    Jekyll Bootstrap 项目在目录中有一篇示例博客文章 posts core samples 我假设 子目录中的帖子 文件 的处理方式与根目录中的帖子相同 它是否正确 如果是这样 我将添加一个 stage 子目录 将其排除 这样我
  • 为什么不使用记录的创建时间作为主键?

    我有一个表 其中有一个自动递增的 PK 和creation date 字段 这是unix 时间戳 我想知道为什么不丢失自动递增字段并使用创建日期字段作为 PK 因为它是唯一的 我使用 1 1000 秒的精度 对于 我正在删除一个索引行 反对
  • ffmpeg - 我可以将音频通道绘制为图像吗?

    我想知道是否可以使用 ffmpeg 将视频或音频文件的音频通道绘制为图像 或者是否有其他工具可以在 Win2k8 x64 上执行此操作 我将这样做作为用户上传视频或音频文件后编码过程的一部分 我使用 ColdFusion 10 来处理上传并
  • 安装 phpredis MAC OSX

    任何人都可以帮我在 MAC OSX 中安装 php redis 吗 brew install php redis 不工作 pecl install php redis 也不起作用 无效的包名称 包文件 php redis 自制错误 自制错误
  • 在 Postgres 中收集递归 JSON 键

    我有 JSON 文档以 JSON 数据类型 Postgres 9 3 存储在 Postgres 中 并且我需要递归地收集树中的键名称 例如 给定这个 JSON 树 files folder file1 property blah file2
  • 更改 EF4 中的数据库表名称(实体框架 4)

    有谁知道如何更改 EF4 实体框架 4 中实体的映射数据库表 稍后编辑 我想我已经在模型浏览器中找到了定义表名称的位置 但它们的名称是只读的 因此无法使用设计器对其进行编辑 另外 在 xml 模式中没有对表名称的引用 根据我的搜索 如果您只
  • unittest 测试用例中的重复代码

    我有一个如下所示的测试用例 def MyTestCase unittest Testcase def test input01 self input read from disk input01 output run input valid
  • 为什么字符范围类 [A-z] 匹配下划线?

    在多种语言中尝试过 结果相同 JavaScript 示例 A z test gt true A z test 0 gt false A z test gt false A z test A gt true 为什么第一个病例没有返回false
  • 使用 JavaScript 调整图像大小并保存到磁盘

    我试图将由 JavaScript 完成的调整大小的图像上传到服务器 所以我尝试使用调整大小的图像设置文件输入 后来我才知道除非用户选择文件 否则我们无法更改文件输入 https stackoverflow com questions 169
  • 如何在服务器上定位Sh​​arepoint文档库源页面?

    我正在使用 Sharepoint 文档库 并且正在尝试查找文档库页面的源 我正在 Sharepoint 服务器上工作 我就是找不到它 它应该存放在哪里 谢谢你 SharePoint 不会将页面直接存储在文件系统中 该机制有点不那么简单 要了
  • 多线程游戏 - 更新、渲染以及如何拆分它们

    所以 我正在开发一个游戏引擎 并且已经取得了很好的进展 然而 我的引擎是单线程的 将更新和渲染分成单独的线程的优点听起来是一个非常好的主意 我该怎么做 单线程游戏引擎 概念上 非常容易制作 你有一个更新 gt 渲染 gt 睡眠 gt 重复的
  • Docker:b'json:无法将数字解组到字符串类型的 Go 结构字段 LogConfig.Config 中

    我在 docker compose 中将我的应用程序配置为使用轮换日志 遵循以下站点中的操作 https medium com Quigley Ja rotating docker logs keeping your overlay fol
  • Maven资源过滤

    我想将构建信息写入属性文件 我找到了 Maven 资源过滤插件 这就是我的 pom 相关部分的样子