gpt4 book ai didi

java - 不用修改原代码,用Maven替换JAVA代码中的一个常量

转载 作者:行者123 更新时间:2023-12-04 13:54:34 29 4
gpt4 key购买 nike

我在 java 常量中有一个文本,我想根据以下方式生成 Artifact 时配置的 maven 变量替换它:

public class FOO {
public static final String BASE = "/@FOO@";
}
问题是如果我替换java代码,它会被永久替换并且不再执行替换,所以如果我改变变量的值它没有效果:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>@FOO@</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</plugin>
我通过反向执行这个过程来解决这个问题:
 <plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>first-execution</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>@FOO@</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</execution>

<execution>
<id>second-execution</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${basedir}/src/main/java/com/my/package/Constants.java</include>
</includes>
<replacements>
<replacement>
<token>${my.custom.property}</token>
<value>@FOO@</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>
但是这第二步可能很危险,因为可能会发生冲突并替换类的 java 代码中具有相同值的内容。
另一种选择是替换 .class 文件,如下所示:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${basedir}/target/my-artifact-directory/WEB-INF/classes/com/my/package//Constants$PATHS.class</include>
</includes>
<replacements>
<replacement>
<token>@FOO@</token>
<value>${my.custom.property}</value>
</replacement>
</replacements>
</configuration>
</plugin>
替换有效,但应用程序无法正常启动。
关于如何在不修改原始代码的情况下执行替换的任何其他想法?

最佳答案

我会在 src/main/resources 中创建一个资源文件在 FOO 中读取并使用 Maven 资源插件的资源过滤类似于对 spring maven profile - set properties file based on compilation profile 的回答:foo.properties

BASE=${propertyName}
pom.xml
<project>
...
<properties>
<propertyName>property value</propertyName>
</properties>

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

关于java - 不用修改原代码,用Maven替换JAVA代码中的一个常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64703838/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com