0

I have a project like this:

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)

and pom.xml like this:

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

After I run mvn package it creates META-INF and WEB-INF folders inside root/target folder and also places .war files in there.

I want maven to also copy a WEB-INF folder from target into the root (TestArt) once the packaging is done. Is there any way to achieve it?

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 1
    Trust me: you do not want that. Builds should only create/alter files in `target`, nowhere else. What is your aim? – J Fabian Meier Feb 16 '19 at 17:36
  • I've run into a problem I described [here](https://stackoverflow.com/questions/54718941/tomcat-servlet-404-status-when-src-main-webapp-web-inf-and-no-error-when-root-w) (tl;dr - unless `WEB-INF` is inside root folder, I can't access servlets), so I'm trying to make a temporary solution (namely placing `WEB-INF` into root which works right now) work, until I find a permanent correct one. – parsecer Feb 16 '19 at 17:38
  • Follow the convention over configuration...first put your resources into `src/main/webapp/WEB-INF` and your usual source code into `src/main/java` etc. and remove the `..` configuration cause it's wrong in this scenario furthermore remove maven-dependency-plugin and the copy goal etc. let spring-boot-maven-plugin do it's work... – khmarbaise Feb 17 '19 at 09:56
  • Another thing remove the class files from `webapp/WEB-INF/classes/com/web/LoginServlet.class` which is caused by your strange configuration via `..`... – khmarbaise Feb 17 '19 at 09:58

2 Answers2

2

I really would recommend to spend time on fixing the original problem, but if you want:

Use Maven Antrun Plugin to copy a folder from one place to another, as described here:

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

The details of the copy command in Ant are explained here:

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

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
1

Couldn't figure it out with Ant, so I used this answer. In my case it would look like this:

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

EDIT: This plugin copy pastes files one by one, instead of taking a folder and copy pasting it with all it files once. This leads to broken links between classes and directories and nothing seems to work. For now, I'm using the batch script:

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

Using this answer's Maven plugin:

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

I managed to achieve a desired result.

But if there are solutions in pure Maven that would copy a directory all at once, I'd love to hear them.

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • Turns out there's something wrong with this approach - although copying of files works fine, I get a `404` error when using this approach. However if I copypaste `WEB-INF` manually (or copypaste all files in root's `WEB-INF` with target's `WEB-INF`) , everything works fine. – parsecer Feb 16 '19 at 21:44
  • If I take `lib` folder from target's `WEB-INF` and replace root's `WEB-INF` with that folder, I can get to the `index.jsp` page, but my servlets break with error: `java.lang.ClassFormatError: Incompatible magic value 4022320623 in class file`. – parsecer Feb 16 '19 at 21:47
  • It looks like some connections between the `.class` files and libraries break this way - because this plugin copies files one by one and not the whole directories at once. – parsecer Feb 16 '19 at 21:50
  • Do not do this cause it's the wrong path..see my comments. – khmarbaise Feb 17 '19 at 09:56