Java build time constant configuration

2019-02-19 10:01发布

I have a project I would like to build using multiple configurations. I have a constant that needs to be different between builds but I do not know how to change it based on my config.

For example I would like to be able to do the following based off a value in a config file.

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

and

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

We use ant for building.

1条回答
神经病院院长
2楼-- · 2019-02-19 10:41

I would advise attempting to emulate Maven resource filtering and profile properties

Source filtering

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>

Notes:

  • The ANT copy task is capable of performing template substitutions.

Build profiles

Property files

Each configuration has a different property file

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"/>

Choosing an alternative build profile

ant -Dprofile=qa ..
查看更多
登录 后发表回答