How to include maven dependency libraries in war f

2019-09-05 17:28发布

I'm trying to include the maven dependencies files while build WAR file through ant. Here is the pom.xml. I've tried multiple solutions including executions etc but nothing works.

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

Here is my build.xml file. Every thing is working fine but I want all dependencies file in my WAR file

    <?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>

Any help highly appreciated.

2条回答
小情绪 Triste *
2楼-- · 2019-09-05 17:44

The war task has a "lib" attribute that you can use to specify a directory of dependencies to include in the war:

<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>
查看更多
三岁会撩人
3楼-- · 2019-09-05 17:49

After searching I found that if I put my dependency folder in on web deployment assembly. It will copy the libraries from there.

查看更多
登录 后发表回答