xml源文件不包含在classpath

xml源文件不包含在classpath

本文介绍了使用maven - clean包,xml源文件不包含在classpath中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以似乎默认情况下,Maven软件包是在java类路径中进行编译而不是复制xml文件,除非它们已经存在。在Eclipse中,我创建了最简单的测试,一个默认的spring mvc模板应用程序。 java源码中只有两个文件,一个java控制器和一个虚拟的xml文件。 pom是:

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
<属性>
< 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>
<依赖关系>
<! - Spring - >
<依赖关系>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> $ {org.springframework-version}< / version>
<排除>
<! - 排除Commons登录SLF4j - >
< exclude>
< groupId> commons-logging< / groupId>
< artifactId> commons-logging< / artifactId>
< / exclusion>
< / exclusions>
< / dependency>
<依赖关系>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< version> $ {org.springframework-version}< / version>
< / dependency>

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

<! - 记录 - >
<依赖关系>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-api< / artifactId>
< version> $ {org.slf4j-version}< / version>
< / dependency>
<依赖关系>
< groupId> org.slf4j< / groupId>
< artifactId> jcl-over-slf4j< / artifactId>
< version> $ {org.slf4j-version}< / version>
< scope>运行时< / scope>
< / dependency>
<依赖关系>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-log4j12< / artifactId>
< version> $ {org.slf4j-version}< / version>
< scope>运行时< / scope>
< / dependency>
<依赖关系>
< groupId> log4j< / groupId>
< artifactId> log4j< / artifactId>
< version> 1.2.15< / version>
<排除>
< exclude>
< groupId> javax.mail< / groupId>
< artifactId> mail< / artifactId>
< / exclusion>
< exclude>
< groupId> javax.jms< / groupId>
< artifactId> jms< / artifactId>
< / exclusion>
< exclude>
< groupId> com.sun.jdmk< / groupId>
< artifactId> jmxtools< / artifactId>
< / exclusion>
< exclude>
< groupId> com.sun.jmx< / groupId>
< artifactId> jmxri< / artifactId>
< / exclusion>
< / exclusions>
< scope>运行时< / scope>
< / dependency>

<! - @Inject - >
<依赖关系>
< groupId> javax.inject< / groupId>
< artifactId> javax.inject< / artifactId>
< version> 1< / version>
< / dependency>

<! - Servlet - >
<依赖关系>
< groupId> javax.servlet< / groupId>
< artifactId> servlet-api< / artifactId>
< version> 2.5< / version>
< scope>已提供< / scope>
< / dependency>
<依赖关系>
< groupId> javax.servlet.jsp< / groupId>
< artifactId> jsp-api< / artifactId>
< version> 2.1< / version>
< scope>已提供< / scope>
< / dependency>
<依赖关系>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< version> 1.2< / version>
< / 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>
<执行>
< 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>

当我在Eclipse(project-> clean)中构建项目时,java类和xml文件在类文件夹中输出。如果我运行一个maven - >软件包,一切也很好,所有的文件仍然在最后的战争。但是如果我运行一个maven - > clean包,Maven似乎无法在类路径中获取xml文件,因为它在classes文件夹和war中缺少。我有使用Springsource Toolsuite 2.8(eclipse靛蓝),Maven 3.0.2

还有什么额外的东西可以让Maven做到这一点吗?

解决方案

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




  • 将所有资源文件(不同于Java文件)放入src / main /资源 - 推荐

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

     <建立> 
    < resources>
    < resource>
    < directory> src / main / java< / directory>
    < includes>
    < include> ** / *。xml< / include>
    < / includes>
    < / resource>
    < resource>
    < directory> src / main / resources< / directory>
    < / resource>
    < / resources>
    < / build>



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:

<?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?

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

解决方案

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

  • 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>
    

这篇关于使用maven - clean包,xml源文件不包含在classpath中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 06:52