eclipse 使用 maven-clean 包,类路径中不包含 xml 源文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9798955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With maven - clean package, xml source files are not included in classpath
提问by chrismarx
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 -> 包,一切也都很好,所有文件仍处于最终War中。但是,如果我运行 maven -> clean 包,Maven 似乎无法在类路径中提取 xml 文件,因为它在 classes 文件夹和 war 中丢失。是否需要做一些额外的事情才能让 Maven 做到这一点?
I'm using Springsource Toolsuite 2.8 (eclipse indigo), Maven 3.0.2
我正在使用 Springsource 工具套件 2.8 (eclipse indigo), Maven 3.0.2
回答by Andrzej Jozwik
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
Add to your pom (example from gwt plugin):
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
- 把你所有的资源文件(不同于 java 文件)放到“src/main/resources”——推荐
添加到您的 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>
回答by nwinkler
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 只会编译其中的 Java 源代码,src/main/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
,则可以将其添加resource
到 POM的部分中。我建议不要这样做,尝试将您想要的非 Java 文件保留在src/main/resources
.