0

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.

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
Chad Kemp
  • 139
  • 1
  • 1
  • 9
  • Glad to have helped :) If you found my answer to be useful please consider marking it as "accepted". If you have questions or need an elaboration, feel free to post. Again, glad to have helped :) – lorinpa Feb 17 '14 at 13:58
  • 2
    To your Q: "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." - Build intelligence in your ant targets to use feature of using reading or evaluting properties like `ant -f build.xml -Denv.value=[DEV,PROD,...] -propertyfile=.xml` –  Feb 17 '14 at 14:29
  • Again, thank you so much for marking my answer as accepted :) Glad to have helped :) Good Luck. – lorinpa Feb 21 '14 at 15:47

1 Answers1

0

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.

lorinpa
  • 556
  • 1
  • 5
  • 6
  • Sorry for the delay. To clarify, i would like the whole build process to run for a specific target environment just by passing a parameter or two to ANT. So if, from my workstation, i want to make and package a build for production, with the build process making all of the appropriate variable substitutions in my properties files, and so forth, i can do that. Knowing that the resultant WAR is suitable for our production environment. This process should compile classes as well. it would be reasonable to expect that not everyone had auto-build turned on in Eclipse or IntelliJ. – Chad Kemp Feb 20 '14 at 06:00
  • 1
    Thank you for the response. I updated my answer. It sounds like you want who a copy of the build file generated by Eclipse or IntelliJ to literally post a modified script. I don't have a copy of your build script or you requirements. I can only show you how to perform the tasks asked: 1) How to "war" a web app with ant. 1) How to use command line arguments with ant. I answered both questions :) Please mark my answer as accepted :) – lorinpa Feb 20 '14 at 17:18
  • Sorry!! Typo in the above comment. I meant to say: It sounds like you want someone to gain access to a build script generated by Eclipse or IntelliJ and make the appropriate modifications. --- That may be a little too much :) Please consider scaling back your expectation :) Perhaps I am wrong :) – lorinpa Feb 20 '14 at 18:47