【问题标题】:Java build time constant configurationJava构建时间常数配置
【发布时间】:2012-10-08 22:54:11
【问题描述】:

我有一个想要使用多种配置构建的项目。 我有一个常量,需要在构建之间有所不同,但我不知道如何根据我的配置更改它。

例如,我希望能够根据配置文件中的值执行以下操作。

@WebService(targetNamespace = "http://example.com/")
public class CustomerWebService {

@WebService(targetNamespace = "http://demo.example.com/")
public class CustomerWebService {

我们使用 ant 进行构建。

【问题讨论】:

    标签: java ant annotations constants final


    【解决方案1】:

    我建议尝试模拟 Maven 资源过滤和配置文件属性

    来源过滤

    src/templates/MyFile.java

    ..
    @WebService(targetNamespace = "@WS_NAMESPACE@")
    public class CustomerWebService {
    ..
    

    build.xml

    <target name="filter-sources">
        <copy todir="${build.dir}/src">
           <fileset dir="src/templates" includes="**/*.java"/>
           <filterset>
              <filter token="WS_NAMESPACE" value="${ws.namespace}"/>
           </filterset>
        </copy>
    </target>
    
    <target name="compile" depends="filter-sources">
        <javac destdir="${build.dir}/classes">
            <src path="src/java"/>
            <src path="${build.dir}/src"/>
            <classpath>
            ..
            ..
        </javac>
    </target>
    

    注意事项:

    • ANT 复制任务能够执行模板替换。

    构建配置文件

    属性文件

    每个配置都有不同的属性文件

    src/properties/dev.properties
    src/properties/qa.properties
    src/properties/prod.properties
    ..
    

    build.xml

    <property name="profile" value="dev"/>
    <property file="src/properties/${profile}.properties"/>
    

    选择替代构建配置文件

    ant -Dprofile=qa ..
    

    【讨论】:

    • 虽然它有效,但我的建议是在您的应用程序中避免这种“特定于环境的编译”。您最终会遇到为不同环境多次构建的情况,而且您部署到生产的工件与您测试的工件不同。
    • @AdrianShum 是的,你说得很对。一个非常常见的构建反模式是为每个部署构建一个特殊文件。但是,有时我需要构建客户特定的工件(不同的许可证密钥、图标等),这确实证明了这项工作是合理的。总之,这篇文章的主要目的是演示如何在 ANT 中模拟 Maven 功能(如源过滤和构建配置文件)。
    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多