使用 Ant 创建战争时不会复制 JSP [英] JSP is not getting copied while creating war using Ant

查看:20
本文介绍了使用 Ant 创建战争时不会复制 JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下 Ant 脚本来创建一个简单的 Web 应用程序战争.

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

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="war">
    <path id="compile.classpath">
        <fileset dir="WebContent/WEB-INF/lib">
            <include name="*.jar" />
        </fileset>
    </path>
    <target name="compile">
        <javac destdir="WebContent/WEB-INF/classes" debug="true" srcdir="src">
            <classpath refid="compile.classpath" />
        </javac>
    </target>
    <target name="war" depends="compile">
        <war destfile="build/myproject.war" webxml="WebContent/WEB-INF/web.xml">
            <fileset dir="WebContent">
                <include name="**/*.jsp" />
            </fileset>
            <lib dir="WebContent/WEB-INF/lib" />
            <classes dir="WebContent/WEB-INF/classes" />
        </war>
    </target>
</project>

它正在创建战争,但是当我打开战争时,由于应用程序没有运行,它不包含 JSP 文件.知道出了什么问题吗?另外,现在我正在 Weblogic 中手动应对战争.有没有可以部署战争的Ant命令?

It's creating the war but when I am opening the war, it's not containing JSP files due to which application is not running. Any idea what is wrong? Also, right now I am coping war manually in Weblogic. Is there any Ant command which can deploy war?

推荐答案

我不知道确切的答案,但这里是我将 Ant build.xml 用于 webapps 的方法.试一试.这适用于 Eclipse 或从命令行运行.几个关键点是:

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 引用了编译时库,包括 servlet-api.jar
  • 动态 META-INF/MANIFEST.MF
  • 编译、jar 和战争任务的单独目标,以便更轻松地按项目自定义规则
  • webapp war 没有单独的 .class 文件,而是编译了 web-inf/lib/mywebapp.jar 库以最小化文件系统噪音
  • 您可以创建 web/WEB-INF/classes/文件夹并放置一些 .properties 文件或极端情况下的二进制提供"类文件.它们与其他 jsp、html、js 文件一起放在 war 包中.
  • 文件夹结构非常精简,我在开发的时候可以直接在Tomcat服务中使用mywebapp/web/文件夹.每个 html、jsp 等更改都会在运行时反映出来.编译 jar 会触发 Tomcat 重新加载 webapp 实例.

为 webapp 项目使用此通用文件夹结构.
/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

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  

构建脚本创建 war 包,并且 web-inf/lib/mywebapp.jar 中的 manifest.mf 更新为具有构建号、标题和版本.非常方便,您可以将文件夹内容用作新 webapp 项目的模板.只需编辑 build.xml 即可获得新的项目名称.

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.

一些编译时依赖指向 mywebapp/web-inf/lib 文件夹.非 war 打包的库仅在编译时放入 mywebapp/lib/文件夹.我喜欢将每个依赖项保留在项目版本控制中,这就是这个 lib 文件夹的原因.您可以使用 *.jar 通配符 ant 语法,但我明确列出了每个文件以用于自我文档目的.

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.

这是在开发期间在 Tomcat 中使用的额外文件.它在 Tomcat 上发布 webapp,并且可以立即看到项目文件夹中的任何更改,这对于客户端文件更改(html、js、jsp)非常方便.

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).

  • 此文件是从 mywebapp/web/META-INF/context.xml 文件复制粘贴的,但添加了显式 docBase 属性.
  • 它指示Tomcat直接使用项目文件夹中的文件,运行时无需重新部署
  • 启动 tomcat 并保持运行,您可以使用同一个 Tomcat 实例运行多个 webapp 项目.有时更大的开发项目需要它.
  • 远程调试钩子需要一些这里没有包含的 Java 魔法

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 构建系统都很好.随心所欲.

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

这篇关于使用 Ant 创建战争时不会复制 JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆