24

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?

thanks, rand

UPDATE: in my pom now I use this:

<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>war</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>myapp/target/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

and I launch mvn with:

mvn -Dmaven.test.skip=true clean package resources:copy-resources

but I got:

[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'

[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:

<configuration>
  ...
  <outputDirectory>VALUE</outputDirectory>
</configuration>.

[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:

<configuration>
  ...
  <resources>VALUE</resources>
</configuration>.

I'm using maven 2.2 and the snippet basically is the same of the documentation any idea?

chrki
  • 6,143
  • 6
  • 35
  • 55
Randomize
  • 8,651
  • 18
  • 78
  • 133
  • don't run the `resources:copy-resources` goal, it's automatically included in phase `copy-resources` (down there in my answer) – ryskajakub Jan 21 '11 at 17:10

3 Answers3

31

either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory. resource plugin

EDIT:

This configuration is working for me:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
      <execution>
        <id>default-copy-resources</id>
        <phase>process-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <overwrite>true</overwrite>
          <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
          <resources>
            <resource>
              <directory>${project.basedir}/src/main/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.

Gray
  • 115,027
  • 24
  • 293
  • 354
ryskajakub
  • 6,351
  • 8
  • 45
  • 75
  • One can use as well the `${project.build.finalName}` instead of `${project.artifactId}-${project.version}` to get the name of the artifact. – ivanjovanovic Feb 07 '18 at 12:28
28

Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml

kc2001
  • 5,008
  • 4
  • 51
  • 92
kan
  • 28,279
  • 7
  • 71
  • 101
  • But, how to deal with `java resource`, if it is not auto deployed to its required place? – Eddy Dec 13 '16 at 12:18
  • @Eddy If it is not deployed into the "required place", most probably it means you put it in the wrong place of a source code. Give more details. – kan Dec 13 '16 at 20:35
  • You are right! In this case: when using maven and run `mvn clean`. then maven will not redeploy it for me when put resources under `src/main/java` instead of `src/main/resources`. I'm using eclipse. – Eddy Dec 15 '16 at 00:29
0

This configuration is working add plugin pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>${jdk.version}</source>
        <target>${jdk.version}</target>
    </configuration>
</plugin>

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

    <configuration>

        <webResources>
              <!--copy resource file location-->
            <resource>
                <directory>${project.build.directory}/classes</directory>
            </resource>
        </webResources>
        <!--location for add file-->
        <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
    </configuration>
</plugin>
vinS
  • 1,417
  • 5
  • 24
  • 37
  • Using maven [*WAR Plug*](https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html) to copy any web resources is the bast way. – Yash May 31 '19 at 13:00