JSP is not getting copied while creating war using Ant

前端 未结 1 1774
南笙
南笙 2021-01-23 00:11

I am using following Ant script to create a war of simple web application.




        
相关标签:
1条回答
  • 2021-01-23 00:51

    I don't know exact answer but here is my way of using Ant build.xml for webapps. Give it a try. This works inside Eclipse or run from the command line. Few key points are:

    • build.xml has reference to compile-time libraries, including servlet-api.jar
    • dynamic META-INF/MANIFEST.MF
    • separate targets for compile, jar and war tasks to allow easier per project custom rules
    • webapp war don't have individual .class files but compiled web-inf/lib/mywebapp.jar library to minimize filesystem noice
    • you may create web/WEB-INF/classes/ folder and put some .properties file or extreme case "binary provided" class files. They are put inside war package along with other jsp,html,js files.
    • folder structure is very streamlined, I can use mywebapp/web/ folder directly in Tomcat service during development. Each html, jsp etc changes are reflected at runtime. Compiling jar triggers Tomcat to reload webapp instance.

    Use this common folder structure for webapp project.
    /mywebapp/ant.bat
    /mywebapp/build.xml
    /mywebapp/classes/
    /mywebapp/src/
    /mywebapp/src/META-INF/MANIFEST.MF
    /mywebapp/lib/
    /mywebapp/web/
    /mywebapp/web/WEB-INF/web.xml
    /mywebapp/web/WEB-INF/lib/
    /mywebapp/web/META-INF/context.xml

    mywebapp/build.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="mywebapp" default="build" basedir=".">
        <property name="name" value="${ant.project.name}" />
        <property name="classes" value="./classes" />
        <property name="src" value="./src" />
        <property name="webdir" value="./web" />    
        <property name="version" value="1.0"/>
    
        <property environment="env"/>
    
        <path id="libs"> 
            <pathelement location="lib/servlet-api.jar" />
            <pathelement location="web/WEB-INF/lib/somelib1.jar" />
            <pathelement location="web/WEB-INF/lib/somelib2.jar" />
            <pathelement location="web/WEB-INF/lib/gson-2.2.4.jar" />
        </path>
    
        <tstamp>
           <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
        </tstamp>
    
    
        <target name="updatemanifest" description="Update manifest">
           <buildnumber file="build.num"/>
    
           <copy file="${src}/META-INF/MANIFEST.MF" 
              todir="${classes}/META-INF/" overwrite="true" preservelastmodified="true"
           />
    
           <manifest file="${classes}/META-INF/MANIFEST.MF" mode="update">
              <attribute name="Implementation-Version" value="${version}.${build.number} (${TODAY})" />
              <attribute name="Implementation-Title"   value="${name}" />       
           </manifest>      
        </target>
    
        <target name="clean" description="Clean compiled classes">
            <delete dir="${classes}" />
        </target>
    
        <target name="compile" depends="clean" description="Compile classes">
            <mkdir dir="${classes}"/>
            <javac srcdir="${src}" destdir="${classes}" target="1.6" source="1.6" encoding="ISO-8859-1" 
                debug="true" debuglevel="lines,source"
                excludes="" includeantruntime="false" >
                <classpath refid="libs" />
                <compilerarg value="-Xlint:deprecation" />
            </javac>
        </target>
    
        <target name="jar" depends="updatemanifest" description="Create a .jar file">
            <echo message="Build release: ${release}" />        
            <jar
                manifest="${classes}/META-INF/MANIFEST.MF" 
                jarfile="${webdir}/WEB-INF/lib/${name}.jar" >
               <fileset dir="${classes}">
               </fileset>
            </jar>
        </target>
    
        <target name="war" depends="compile,jar" description="Create a .war file">
            <delete file="${name}.war" />
            <zip destfile="${name}.war"
                basedir="${webdir}"
                excludes="
                    **/CVS*
                    "
            />
        </target>
    
        <target name="build" depends="war" description="Build lib">
        </target>
    
    </project>
    

    src/META-INF/MANIFEST.MF

    Implementation-Title: myappname   
    Implementation-Version: 1.0.0 (2010-03-01)   
    Implementation-Vendor: My Name Ltd.   
    Implementation-URL: http://www.myname.com   
    

    mywebapp/build.bat

    call c:\apache-ant-1.7.0\bin\ant.bat build   
    pause  
    

    Build script creates war package and manifest.mf within web-inf/lib/mywebapp.jar is updated to have build number, title and version. Very handy you can use folder content as a template for new webapp projects. Just edit build.xml to have new project name.

    Some compile-time dependencies point mywebapp/web-inf/lib folder. Non war-packaged libraries are put to mywebapp/lib/ folder for compile time only. I like keeping each dependency within project version control so thats a reason for this lib folder. You may use *.jar wildcard ant syntax but I explictly list each file for self documentation purpose.

    Here is a bonus file to be used in Tomcat during development time. It publishes webapp on Tomcat and any changes in project folder is seen immediately, its very handy for client file changes (html,js,jsp).

    • this file is a copypaste from mywebapp/web/META-INF/context.xml file but an explicit docBase attribute is added.
    • It directs Tomcat to use files directly from project folder, no redeployment needed at runtime
    • Start tomcat and keep it running, you may run several webapp projects withing same Tomcat instance. Sometimes bigger development projects need it.
    • Remote debugging hook requires some java magic not included here

    tomcat/conf/Catalina/localhost/mywebapp.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Context docBase="C:/mywebapp/web"
        debug="0" reloadable="true" crossContext="true" >
    
    <!--
      <Valve className="org.apache.catalina.valves.RemoteAddrValve"
        allow="127.0.0.1" />
    -->
    
    <!--
       <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
    -->
    
      <!-- pooled db connection -->
        <Resource name="jdbc/mywebappDB" auth="Container" type="javax.sql.DataSource" 
            maxActive="10" maxIdle="2" maxWait="20000" 
            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
            username="myuserid" password="mypwd" 
            url="jdbc:sqlserver://mysqlserv1.com:1433;DatabaseName=MyDB;applicationName=mywebapp" 
            validationQuery="SELECT 1" 
        />
        <!-- <ResourceLink name="jdbc/mywebappDB" global="jdbc/mywebappDB" type="javax.sql.DataSource" /> -->
    
    
        <Resource name="jdbc/mywebappDB2" auth="Container" type="javax.sql.DataSource"
            maxActive="100" maxIdle="20" maxWait="10000"
            driverClassName="com.mysql.jdbc.Driver"
            username="myuserid" password="mypwd"
            url="jdbc:mysql://localhost:3306/myDB2?useUnicode=true&amp;characterEncoding=utf8"
            validationQuery="SELECT 1" removeAbandoned="true" removeAbandonedTimeout="300"
          />
    
    </Context>
    

    ps: Ant build system is fine no matter what some people may say. Go with it as you please.

    0 讨论(0)
提交回复
热议问题