如何使用Ant构建在war文件中包含maven依赖库

时间:2014-09-18 01:17:17

标签: java eclipse maven ant dependencies

我试图通过ant构建WAR文件时包含maven依赖项文件。这是pom.xml。我尝试了多种解决方案,包括执行等,但没有任何作用。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CustomerPortal</groupId>
<artifactId>CustomerPortal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

        <!-- <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> 
            <configuration> <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> 
            </configuration> </plugin> -->

    </plugins>
</build>
<repositories>
    <repository>
        <id>ZK CE</id>
        <name>ZK CE Repository</name>
        <url>http://mavensync.zkoss.org/maven2</url>
    </repository>
</repositories>
<dependencies>

    <!-- Log4j starts -->

    <!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> 
        <version>2.0.2</version> </dependency> -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.0.2</version>
    </dependency>

    <!-- Log4j ends -->

    <!-- ZK starts -->

    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zkplus</artifactId>
        <version>7.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zhtml</artifactId>
        <version>7.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zkbind</artifactId>
        <version>7.0.2</version>
    </dependency>
    <!-- <dependency> <groupId>org.zkoss.zk</groupId> <artifactId>zkmax</artifactId> 
        <version>6.5.2</version> </dependency> -->
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zul</artifactId>
        <version>7.0.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency>

    <!-- ZK ends -->

    <!-- Hibernate starts -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
        <classifier>tests</classifier>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.1.0.CR2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>2.5.1</version>
    </dependency>
    <!-- Hibernate end -->

    <!-- Email Api starts -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>
    <!-- Email Api ends -->
    <!-- Time API starts -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>
    <!-- Time API ends -->

    <!-- BeanUtils Api starts -->
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.7.0</version>
    </dependency>
    <!-- BeanUtils Api ends -->


    <!-- MYSQL Driver starts -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!-- MYSQL Driver ends -->




</dependencies>

这是我的build.xml文件。每件事都运行正常,但我希望我的WAR文件中的所有依赖项文件

    <?xml version="1.0" encoding="UTF-8"?>
<project name="MedGulfCustomerPortal" default="war" basedir=".">

    <property name="project-name" value="${ant.project.name}" />
    <property name="war-file-name" value="${project-name}.war" />
    <property name="source-directory" value="src" />
    <property name="main-web-directory" value="WebContent" />
    <property name="web-directory" value="WebContent/WEB-INF" />
    <property name="web-xml-file" value="WebContent/WEB-INF/web.xml" />
    <property name="build-directory" value="build" />
    <property name="dir.classes" value="WebContent/WEB-INF/classes" />

    <tstamp prefix="build-info">
        <format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
        <format property="current-time" pattern="hh:mm:ss a z" locale="en" />
    </tstamp>
    <path id="maven-ant-tasks.classpath" path="WebContent/WEB-INF/lib/maven-ant-tasks-2.1.3.jar" />
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" />


    <!-- <javac debug="on" srcdir="${source-directory}" destdir="${dir.classes}" includes="**/*" includeantruntime="false">
            <classpath refid="maven-ant-tasks.classpath" />
        </javac>
    -->
    <target name="war" depends="">
        <mkdir dir="${build-directory}" />
        <delete file="${build-directory}/${war-file-name}" />
        <war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
            <zipfileset dir="${main-web-directory}" includes="**/*zul" />
            <zipfileset dir="${web-directory}" includes="**" prefix="WEB-INF" />
            <zipfileset dir="${source-directory}" includes="**/*java" prefix="WEB-INF/classes" />
            <manifest>
                <attribute name="Built-By" value="${builder}" />
                <attribute name="Built-On" value="${build-info.current-date}" />
                <attribute name="Built-At" value="${build-info.current-time}" />
            </manifest>

        </war>
    </target>

</project>

任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:1)

战争任务有一个&#34; lib&#34;您可以用来指定要包含在战争中的依赖项目录的属性:

<project name="demo" default="build" xmlns:artifact="antlib:org.apache.maven.artifact.ant">

  <available property="maven.avail" classname="org.apache.maven.artifact.ant.DependenciesTask"/>

  <target name="init" unless="maven.avail">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/maven-ant-tasks.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar"/>
    <fail message="Maven tasks installed, run build again"/>
  </target>

  <target name="build" depends="init">
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant"/>

    <artifact:dependencies filesetId="runtime.fileset" useScope="runtime">
      <pom file="pom.xml"/>
    </artifact:dependencies>

    <copy todir="build/lib">
      <fileset refid="runtime.fileset"/>
      <flattenmapper/>
    </copy>

    <war destfile="build/demo.war" webxml="src/resources/web.xml">
      <fileset dir="src/resources" excludes="web.xml"/>
      <lib dir="build/lib"/>
    </war>
  </target>

  <target name="clean">
    <delete dir="build"/>
  </target>

</project>

答案 1 :(得分:-1)

搜索后,我发现如果我将我的依赖文件夹放在Web部署程序集中。它将从那里复制库。