Maven:打包文件夹后,将粘贴WEB-INF复制到根目录中

时间:2019-02-16 17:24:48

标签: java maven tomcat java-ee ant

我有一个像这样的项目:

TestArt
-----------index.jsp
-----------pom.xml
-----------src
--------------main
------------------java/com/web/LoginServlet.java
------------------webapp/WEB-INF/classes/com/web/LoginServlet.class
-----------target
-----------------testproject-1.0-SNAPSHOT.war
-----------------testproject-1.0-SNAPSHOT.war.original
-----------------testproject-1.0-SNAPSHOT
----------------------------------META-INF
----------------------------------WEB-INF
---------------------------------------classes/com/web/LoginServlet.class
---------------------------------------lib (here are all the jars)

pom.xml 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.util</groupId>
    <artifactId>testproject</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Test Project</name>
    <url>https://test.com</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <dependencies>
        ...
    </dependencies>

    <properties>
        <java.version>1.7</java.version>
        <start-class>com.web.LoginServlet</start-class>
    </properties>

    <build>
        <outputDirectory>${basedir}\src\main\webapp\WEB-INF\classes</outputDirectory>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals><goal>copy</goal></goals>
                        <configuration>

                            <artifactItems>

                                <artifactItem>
                                    <groupId>com.github.jsimone</groupId>
                                    <artifactId>webapp-runner</artifactId>
                                    <version>8.0.30.2</version>
                                    <destFileName>webapp-runner.jar</destFileName>
                                </artifactItem>

                            </artifactItems>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>

            </plugin>

            <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

        </plugins>

        <defaultGoal>install</defaultGoal>
    </build>
</project>

运行 mvn软件包后,它将在 root / target 内创建 META-INF WEB-INF 文件夹文件夹,并在其中放置 .war 文件。

我希望maven在打包完成后还将 WEB-INF 文件夹从 target 复制到根目录( TestArt )。有什么办法可以实现?

2 个答案:

答案 0 :(得分:2)

我真的建议您花些时间解决原始问题,但是如果您愿意的话:

使用Maven Antrun插件将文件夹从一个位置复制到另一个位置,如下所述:

https://stackoverflow.com/a/694175/927493

Ant中复制命令的详细信息在这里说明:

https://ant.apache.org/manual/Tasks/copy.html

答案 1 :(得分:1)

无法用Ant来解决,所以我使用了this answer。就我而言,它看起来像这样:

  <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
            <execution>
                <id>copy-resources</id>
                <!-- here the phase you need -->
                <phase>validate</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${basedir}/WEB-INF</outputDirectory>
                    <resources>
                        <resource>
                            <directory>target/testproject-1.0-SNAPSHOT/WEB-INF</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
  </plugin>

编辑:此插件可复制文件一个接一个地粘贴文件,而不是复制文件夹并将其所有文件粘贴一次。这导致类和目录之间的链接断开,并且似乎没有任何效果。现在,我正在使用batch脚本:

xcopy "C:\...\Tomcat 8.5\webapps\TestArt\target\testproject-1.0-SNAPSHOT\WEB-INF" "C:\...\Tomcat 8.5\webapps\TestArt\WEB-INF\" /E /Y

使用this answer's Maven插件:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>sign</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <exec executable="${basedir}\copy.bat">

                            </exec>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我设法取得了预期的结果。

但是,如果纯Maven中有解决方案可以一次复制一个目录,那么我很想听听他们。