2

I've got the following project structure:

src/
  main/
    java/
      ...
    resources/
      META-INF/
        context.xml
        persistence.xml
    webapp/
      WEB-INF/
        web.xml
        ...
      ...
  test/
    ...

pom.xml (specifically, build part) looks like this:

<build>
<finalName>...</finalName>

<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
      <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
      <containerConfigXML>src/main/resources/META-INF/context.xml</containerConfigXML>
    </configuration>
  </plugin>
</plugins>
</build>

The trouble is that context.xml gets doubled in the resulting WAR. It appears in

  1. /META-INF - that's the right place for it, and
  2. /WEB-INF/classes/META-INF. I see it gets copied here from the resources folder, as any other resource.

Application deploys flawlessly, but this duplication really bothers me. What's wrong with my setup?

4

3 回答 3

1

Add a <resources> element under <build>, and explicitly exclude the file.

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>META-INF/context.xml</exclude>
        </excludes>
    </resource>
</resources>
于 2013-05-14T16:28:10.553 回答
0

n general, for a Java-based Maven project, non-source files should go in the src/main/resources sub-directory of the project. The contents of that resources directory are copied to the output directory (by default, target/classes) during the process-resources phase of the build.

For Maven WAR projects, it is slightly more complicated: there is also the src/main/webapp directory, wherein Maven expects to find WEB-INF/web.xml

As the WEB-INF directory must exist under src/main/webapp, I'd recommend avoiding defining it again in src/main/resources. Although this is perfectly valid and the contents of the two directories will be merged, it can get confusing if a file is defined in both. The contents of src/main/resources will take precedence as they are copied over the top of the contents from src/main/webapp.

Hence you are seeing the duplication.

于 2013-05-14T12:22:38.570 回答
0

For WAR builds maven uses following defaults:

src/main/java goes to /WEB-INF/classes
src/main/resources goes to /WEB-INF/classes
src/main/webapp goes to /

So in order to quickly fix your problem, just move META-INF from src/main/resources to src/main/webapp

Also make sure you updated containerConfigXML

于 2013-05-14T18:00:58.663 回答