使用maven-clean包,xml源文件不包含在类路径中

时间:2023-01-25 14:15:11

So it seems that Maven package is setup by default to compile but not copy xml files in the java classpath, unless they're already there. In Eclipse, I created the simplest test, a default spring mvc template app. There are only two files in the java source, a java controller and dummy xml file. The pom is:

所以看起来Maven包默认设置为编译但不复制java类路径中的xml文件,除非它们已经存在。在Eclipse中,我创建了最简单的测试,一个默认的spring mvc模板应用程序。 java源代码中只有两个文件,一个java控制器和一个虚拟xml文件。 pom是:

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <name>abc</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>3.0.6.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.9</org.aspectj-version>
        <org.slf4j-version>1.5.10</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>   

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>abc</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

When I build the project in Eclipse (project->clean), both the java class and xml file are output in the classes folder. If I run a maven -> package, everything is also just fine, all files still in final war. But if I run a maven -> clean package, Maven does not seem to be able to pickup the xml file in the classpath, as it's missing in the classes folder and war. Is there something extra that has to be done to get Maven to do this?

当我在Eclipse中构建项目(project-> clean)时,java类和xml文件都在classes文件夹中输出。如果我运行maven - >包,一切都很好,所有文件仍处于最后的战争状态。但是如果我运行maven - > clean包,Maven似乎无法在类路径中拾取xml文件,因为它在classes文件夹和war中都没有。为了让Maven这样做,还有什么必须做的事吗?

I'm using Springsource Toolsuite 2.8 (eclipse indigo), Maven 3.0.2

我正在使用Springsource Toolsuite 2.8(eclipse indigo),Maven 3.0.2

2 个解决方案

#1


40  

By default maven does not include any files from "src/main/java" (${sourceDirectory} variable). You have two possibilities (choose one of them):

默认情况下,maven不包含“src / main / java”($ {sourceDirectory}变量)中的任何文件。你有两种可能性(选择其中一种):

  • put all your resource files (different than java files) to "src/main/resources" - recomended
  • 把你所有的资源文件(不同于java文件)放到“src / main / resources” - 推荐
  • Add to your pom (example from gwt plugin):

    添加到您的pom(例如来自gwt插件):

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>                      
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    

#2


6  

The xml file shouldn't be placed in src/main/java but in src/main/resources instead. Maven will only compile java sources in src/main/java and will ignore everything else in there by default.

不应将xml文件放在src / main / java中,而应放在src / main / resources中。 Maven只会在src / main / java中编译java源代码,默认情况下会忽略其中的所有内容。

If you want to keep the XML in src/main/java, you can add it in the resource section of the POM. I would recommend against this, try to keep non-java files that you want in the artifact in src/main/resources.

如果要将XML保留在src / main / java中,可以将其添加到POM的资源部分。我建议不要这样做,尝试在src / main / resources中的工件中保留你想要的非java文件。

#1


40  

By default maven does not include any files from "src/main/java" (${sourceDirectory} variable). You have two possibilities (choose one of them):

默认情况下,maven不包含“src / main / java”($ {sourceDirectory}变量)中的任何文件。你有两种可能性(选择其中一种):

  • put all your resource files (different than java files) to "src/main/resources" - recomended
  • 把你所有的资源文件(不同于java文件)放到“src / main / resources” - 推荐
  • Add to your pom (example from gwt plugin):

    添加到您的pom(例如来自gwt插件):

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>                      
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    

#2


6  

The xml file shouldn't be placed in src/main/java but in src/main/resources instead. Maven will only compile java sources in src/main/java and will ignore everything else in there by default.

不应将xml文件放在src / main / java中,而应放在src / main / resources中。 Maven只会在src / main / java中编译java源代码,默认情况下会忽略其中的所有内容。

If you want to keep the XML in src/main/java, you can add it in the resource section of the POM. I would recommend against this, try to keep non-java files that you want in the artifact in src/main/resources.

如果要将XML保留在src / main / java中,可以将其添加到POM的资源部分。我建议不要这样做,尝试在src / main / resources中的工件中保留你想要的非java文件。