Question

This might seem a trivial question, but I have Googled for about 1 hour and not found an answer that directly addresses it, so here goes:

Stack:

  • Java
  • Tomcat7
  • MySQL
  • OpenDS

Development Environment IntelliJ IDEA (personal) - Some of our developers prefer Eclipse

I have a Java/Spring project that we deploy to multiple environments and I am trying to create a build process for us developers to use that will allow us to create a deployable WAR file from our workstation. Eg., I want to be able to build a WAR that I can then, either through ANT or manually, deploy to the Servlet container for:

  1. Sandbox - a build that will run on the given workstation with local Tomcat, etc.
  2. DEV - From my workstation, build a WAR that can be sent to another team to deploy on managed DEV servers
  3. QA - Same as DEV with obvious differences in replacement variables like other server names that are referenced, passwords passed as their encrypted strings, etc.
  4. PROD - same as QA...

What I would like to be able to do is, either from the command line or through IntelliJ, specify which environment I want to package a WAR file for, and run it. Then take the WAR and do what I need to do with it.

Again, I know this sounds trivial, and there is something very elemental about ANT that I am missing, but any help you can provide is greatly appreciated.

Was it helpful?

Solution

here is a skeleton ant build with 2 targets. Init simple echoes to the console your class paths (used to debug). The "make-war" task build the actual war archive.You will need to change the locations to match your setup.

<project name="Sample Build Script" default="init" basedir=".">

            <property environment="env" /> 
            <!-- ***** COMMAND LINE ARGUMENTS DEMOED HERE -->
            <property name="build_type" value= "${env.build_type}"/>
    <property name="version" value="${env.version}"/> 
            <!-- ***** END OF COMMAND LINE ARG **** -->                 

            <property name="src.dir" value="${basedir}/source"/> 
            <property name="build.classes.dir" value="${basedir}/classes"/>
            <property name="project.name" value="myproject"/>

               <target name="make-war" depends="compile-servlet">
                   <delete file="${build.classes.dir}/war/${project.name}.war"/>    
                   <war destfile="${build.classes.dir}/war/${project.name}.war" webxml="${src.dir}/WEB-INF/web.xml">    
                   <webinf dir="${src.dir}/WEB-INF" />    

                   <fileset dir="${src.dir}/html">    
                     <include name="*.html" />
                  </fileset>

                  <classes dir="${build.classes.dir}">    
                    <include name="/my/package/*.*"/>
                  </classes>   

                  <lib dir="/some/lib/loc">
                    <include name="some-lib.jar"/>
                       </lib>
                 </war>    
            </target>   

            <target name="init" > 
               <echo message="Using Source directory=${src.dir}" />  
               <echo message="Using  Build-Classes directory=${build.classes.dir}" />  
               <!-- **** VERIFY COMMAND LINE ARGS HERE ***** -->
               <echo message="Build Type=${build_type}" />  
               <echo message="Build Version =${version}" />  
               <!-- *** END VERIFY COMMAND LINE ARGUMENTS -->
            </target>
       </project>

I am assuming you have already compiled you classes. You simply want to generate the war archive. I included the html and lib elements as a reference. You can always comment them out. Hope that helps :)

UPDATE: I added to COMMAND LINE VARIABLES. You save the above in a file called build.xml. You run the file with the following command line:

$> ant -Denv.build_type=PROD -Denv.version=2.01

That sets your build type to "PROD" and version to "2.01". That demonstrated how to use command line arguments in an ANT BUILD FILE (per your elaboration).

For someone to incorporate, the command line processing and the "war" target, they need access to your specific build file, build requirements, environment etc. While it is true that the term "PRODUCTION" is common. What "PRODUCTION" means varies from project to project, etc. Thus,all someone can do is show you how to create a ANT script which accepts arguments.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top