user1737609
user1737609

Reputation: 21

Replacing a string in jnlp when generating war using ant build

Can anyone help me how to replace a string in jnlp while building the application. Here is the steps I follow.

  1. Create a jnlp file with string content like

    <jnlp spec="1.0+" codebase="%$%test.url$" href="test.jnlp">
    
  2. And our application is environment dependent, While generating war , i'll pass a command line argument to ant build. So based on this command line argument we do maintain a different URL in property file.
  3. while building the application , I couldn't replace the environment dependent URL in jnlp.

Can anyone suggest on this?

Upvotes: 2

Views: 424

Answers (1)

Dr.Haribo
Dr.Haribo

Reputation: 1778

To pass an argument to ant to choose environment you could start ant with, for instance, ant -Denv=prod or ant -Denv=test and make decisions in the ant build.xml based on the env property.

You could set other properties based on the chosen environment like so:

<target name="compile" depends="init" description="compile the source">
  <!-- settings for production environment -->
  <condition property="scalacparams"
             value="-optimise -Yinline -Ydead-code -Ywarn-dead-code -g:none -Xdisable-assertions">
    <equals arg1="${env}" arg2="prod"/>
  </condition>
  <condition property="javacparams" value="-g:none -Xlint">
    <equals arg1="${env}" arg2="prod"/>
  </condition>

  <!-- settings for test environment -->
  <condition property="scalacparams" value="-g:vars">
    <equals arg1="${env}" arg2="test"/>
  </condition>
  <condition property="javacparams" value="-g -Xlint">
    <equals arg1="${env}" arg2="test"/>
  </condition>

  <!-- abort if no environment chosen -->
  <fail message="Use -Denv=prod or -Denv=test">
    <condition>
      <not>
        <isset property="scalacparams"/>
      </not>
    </condition>
  </fail>

  <!-- actual compilation done here ->
</target>

You could also use <if> to perform certain actions only for a certain environment:

  <if> <!-- proguard only for production release -->
    <equals arg1="${env}" arg2="prod" />
    <then>
      <!-- run proguard here -->
    </then>
  </if>

Finally, for inserting a string into a file depending on the environment, first set a property after checking which environment has been chosen (like above), and then:

<copy file="template.jnlp" tofile="appname.jnlp">
  <filterset begintoken="$" endtoken="$">
    <filter token="compiledwith" value="${scalacparams}"/>
    <!--- more filter rules here -->
  </filterset>
</copy>

That's assuming template.jnlp is a file with place holders surrounded by $. In the example $compiledwith$ in template.jnlp would be replaced with the scala compiler parameters set earlier and the result written to appname.jnlp.

Upvotes: 1

Related Questions