【问题标题】:maven classpathentry includes is ignoring pom.xmlmaven classpathentry 包括忽略 pom.xml
【发布时间】:2015-10-03 11:00:22
【问题描述】:

我试图在我的 Maven 项目中将 **/*.java 旁边的 **/*.properties 也包括到我的 .classpath 中,但它根本不起作用。

我试过https://maven.apache.org/plugins/maven-eclipse-plugin/examples/specifying-source-path-inclusions-and-exclusions.html上的教程

但它根本不会影响我的 .classpath 文件中的任何内容。

这是我pom.xml的相关部分:

<project xmlns="http://maven.apache.org/POM/4.0.0"...>
    <build>

    <scriptSourceDirectory>F:\07 Krauck-Systems\Workspace\git\PCMS\KSFramework\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>F:\07 Krauck-Systems\Workspace\git\PCMS\KSFramework\src\com\ks\framework\test</testSourceDirectory>
    <outputDirectory>F:\07 Krauck-Systems\Workspace\git\PCMS\KSFramework\bin</outputDirectory>
    <testOutputDirectory>F:\07 Krauck-Systems\Workspace\git\PCMS\KSFramework\bin\test-classes</testOutputDirectory>

    <sourceDirectory>src</sourceDirectory>

    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>


    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <sourceIncludes>
                    <sourceInclude>**/*.properties</sourceInclude>
                </sourceIncludes>
            </configuration>
        </plugin>
    </plugins>
</build>
[...]
</project>

这是生成的类路径条目:

<classpath>
    <classpathentry including="**/*.java" kind="src" output="bin" path="src">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    [...]
</classpath>

更新:

使用第一个答案中推荐的 pom 后,我得到:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry including="**/*.java" kind="src" path="src/main/resources"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry including="**/*.java" kind="src" path="src/test/resources"/>


    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.USER_LIBRARY/User Libraries"/>
    <classpathentry exported="true" kind="lib" path="libs/collectionUtils/commons-collections4-4.0.jar" sourcepath="libs/collectionUtils/commons-collections4-4.0-sources.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/commons-lang3-3.1.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/commons-net-3.3.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/mailapi.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/smtp.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/mysql-connector-java-5.1.18-bin.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/joda-time-2.2.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/commons-collections4-4.0.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/velocity-1.7.jar"/>
    <classpathentry kind="lib" path="libs/log4j-api-2.0.jar"/>
    <classpathentry kind="lib" path="libs/log4j-core-2.0.jar"/>
    <classpathentry kind="lib" path="libs/junit/hamcrest-all-1.3.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/commons-io-2.4.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
    <classpathentry kind="lib" path="libs/velocity/antlr-2.7.5.jar"/>
    <classpathentry kind="lib" path="libs/velocity/avalon-logkit-2.1.jar"/>
    <classpathentry kind="lib" path="libs/velocity/commons-collections-3.2.1.jar"/>
    <classpathentry kind="lib" path="libs/velocity/commons-lang-2.4.jar"/>
    <classpathentry kind="lib" path="libs/velocity/commons-logging-1.1.jar"/>
    <classpathentry kind="lib" path="libs/velocity/jdom-1.0.jar"/>
    <classpathentry kind="lib" path="libs/velocity/log4j-1.2.12.jar"/>
    <classpathentry kind="lib" path="libs/velocity/maven-ant-tasks-2.0.9.jar"/>
    <classpathentry kind="lib" path="libs/velocity/oro-2.0.8.jar"/>
    <classpathentry kind="lib" path="libs/velocity/servletapi-2.3.jar"/>
    <classpathentry kind="lib" path="libs/velocity/werken-xpath-0.9.4.jar"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

问题是,我还得补充:

|**/*.properties

<classpathentry including="**/*.java|**/*.properties" kind="src" path="src/main/resources"/>

使其工作(识别 config.properties 文件)。不幸的是,它并没有解决我的问题。还有什么问题吗?

【问题讨论】:

  • 哪个 Eclipse 版本?您是否将 M2Eclipse 插件更新到最新版本?

标签: java maven classpath pom.xml


【解决方案1】:

您不应该将maven-eclipse-plugin 用于您的用例。感谢M2Eclipse,Maven 已集成到 Eclipse 中。这其实是在maven-eclipse-pluginhomepage上做广告的:

建议用户使用 m2e,即 Eclipse Maven 集成而不是此插件,因为它更类似于项目 pom.xml 中描述的实际构建和运行时类路径 - 以及其他优点。

阅读您的pom.xml,我注意到您没有遵循 Maven 约定,这是您问题的根源。

我建议您按照以下步骤正确入门:

  • 安装(或更新)M2Eclipse。这可以通过转到“帮助 > 安装新软件...”并选择以下 URL 来完成:http://download.eclipse.org/technology/m2e/releases/
  • 将所有主要 Java 源文件放入文件夹 src/main/java
  • 将所有测试 Java 源文件放入文件夹 src/test/java
  • 将所有主要资源(即属性文件)放在 src/main/resources 中。
  • 将所有测试资源(即属性文件)放在 src/test/resources 中。
  • 用这个简单的 POM 替换您的 POM。

    <project xmlns="http://maven.apache.org/POM/4.0.0"...>
        <build>
            <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
            <plugins>
                <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
  • 通过右键单击项目并转到“Maven > 更新项目...”,在 Eclipse 中更新您的 Maven 项目。

根据经验,您应该尽量遵循这些约定:每个 Maven 开发人员都在使用它们,并且可以让您轻松理解您的构建过程。

【讨论】:

  • 您好,我按照您的建议创建了这个结构,并将 config.properties 放入 src/main/resources。但是在更新后它会创建这个: 并且这不包括 *.properties .还有什么想法吗?
  • @Niko 尽量减少您的 POM(如我的示例),更新 M2Eclipse,从 Eclipse 中选择“Project > Clean”并更新 Maven 项目“Maven > Update Project... ”。在我的机器上,我的答案中的 POM 生成以下类路径条目:
猜你喜欢
  • 2018-08-04
  • 2021-06-19
  • 2015-09-22
  • 2017-03-10
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多