简体   繁体   中英

Duplicated context.xml in Maven project

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?

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>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM