用Maven替换JAVA代码中的某个常量,无需修改原代码

osc*_*car 6 java maven maven-replacer-plugin

我在 java 常量中有一个文本,我想根据在生成工件时配置的 Maven 变量来替换该文本,方法如下:

public class FOO {
    public static final String BASE = "/@FOO@";
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我替换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>
Run Code Online (Sandbox Code Playgroud)

我通过相反的过程解决了这个问题:

 <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>
Run Code Online (Sandbox Code Playgroud)

但是第二步可能很危险,因为可能会发生冲突并替换类的 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>
Run Code Online (Sandbox Code Playgroud)

替换有效,但应用程序无法正确启动。关于如何在不修改原始代码的情况下执行替换还有其他想法吗?

Ger*_*ica 0

我将创建一个资源文件,在src/main/resources其中读取FOO并使用 Maven 资源插件的资源过滤,类似于spring maven profile - set properties file based on Compiling Profile 的答案:

foo.properties

BASE=${propertyName}
Run Code Online (Sandbox Code Playgroud)

pom.xml

BASE=${propertyName}
Run Code Online (Sandbox Code Playgroud)