【问题标题】:Using Eclipse, something keeps adding src/test/java to the classpath使用 Eclipse,一些东西不断将 src/test/java 添加到类路径中
【发布时间】:2017-12-17 09:20:00
【问题描述】:

情况是这样的。我在一个 java/selenium/maven 项目中。我没有测试类,因此 Project->Properties->Java Build Path->Source 中仅有的两个“构建​​路径上的源文件夹”是:

src/main/java 和 src/main/resources。此外,将 src/main/resources “排除”设置为 (None)。

我做一个 Maven/更新项目...

现在由于某种原因 src/test/java 已添加但丢失,并且 src/main/resouces 已更改为 **,它排除了该目录中的所有文件。

我进行更改以重做所有这些、保存、执行 Maven/更新项目,然后再次返回这些更改。是什么不断改变我的项目的 .classpath?

编辑后添加 .settings 文件。

在执行 Maven 之前->更新项目:

<?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 kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="src/main/resources/db2jcc_license.jar"/>
    <classpathentry kind="lib" path="src/main/resources/db2jcc4.jar"/>
    <classpathentry kind="lib" path="src/main/resources/ojdbc6.jar"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

执行 Maven->更新项目后:

<?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 kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="src/main/resources/db2jcc_license.jar"/>
    <classpathentry kind="lib" path="src/main/resources/db2jcc4.jar"/>
    <classpathentry kind="lib" path="src/main/resources/ojdbc6.jar"/>
    <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 kind="output" path="target/classes"/>
</classpath>

注意:从提出的问题中添加了 POM.xml:

    <?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.this.ebusiness</groupId>
    <artifactId>test-automation-framework</artifactId>
    <version>LOCALBUILD</version>
    <packaging>jar</packaging>

    <name>Test Automation Framework</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-safari-driver</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.ini4j</groupId>
        <artifactId>ini4j</artifactId>
        <version>0.5.4</version>
    </dependency>
</dependencies>

【问题讨论】:

    标签: eclipse maven m2eclipse


    【解决方案1】:

    在 Eclipse 中,“Maven - 更新项目...”命令指示 Eclipse 基于 pom.xml 更新其自己的项目文件(.classpath 和可能的其他文件)。它不是一个实际的 Maven 命令。您看到的修改是由 Eclipse 添加的,基于 Java/Maven 项目的典型布局。我相信这是由maven2Naturemaven2Builder.project 中的存在控制的。

    您实际上并没有说明此问题,所以我不确定您为什么要更改它。但您可能会发现这些答案很有帮助:

    What exactly does Maven Update Project do in Eclipse?

    What does Maven Update Project do in Eclipse?

    【讨论】:

    • 一个大问题是它将**添加到/src/main/resources的排除列表中,然后它不会在生成的jar文件中包含这些文件并且它不起作用。另一个问题是它没有成功构建,因为它期望 /src/main/test 目录存在,而在我的项目中不存在。因此,不会创建 jar,并且依赖于该框架的其他项目不会构建或运行。
    • 您将 .classpath(仅对 Eclipse 重要)的目的与 Maven POM 混淆了。排除 ** 只会影响 Eclipse 内部构建器的类路径。当使用例如构建时,它不会排除包含在 JAR 中的资源。 maven install.
    • 至于缺少的/src/main/test 搞砸了你的构建,只需添加一个空目录,Eclipse 就会很高兴。这就是 Eclipse 期望的目录结构的样子。我不知道有什么解决方法。
    • 谢谢库拉!这回答了我的问题。
    【解决方案2】:

    这里有两个问题:

    1) 如果您使用的是 maven,则不应修改项目的 .classpath 或设置文件,因为这将由 eclipse 使用 m2e 插件维护。

    这就是当您使用“maven => update project”时,您的项目文件会从 maven 模板更新的原因。

    2) 使用 maven 是个好主意。 Maven 介绍了一个项目的一些模板。因此,如果您使用的是 maven,您将不得不接受一些 maven 特定的约定。

    这个约定在 maven 项目页面上有描述。

    您可以通过覆盖 maven 超级 pom 来覆盖此约定。

    请看这里: http://maven.apache.org/pom.html#The_Super_POM

    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    

     <testResources>
          <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
          </testResource>
        </testResources>
    

    【讨论】:

    • 问题是,我不是直接编辑 .classpath 或设置文件,而是通过 Eclipse 通过右键单击项目,选择“属性”并在“源”选项卡中进行更改。然后我正在检查生成的 .settings 以查看它的外观。
    • 如果您运行“Maven - 更新项目...”,请确保取消选中“从 pom.xml 更新项目配置”选项。这就是覆盖您对 .classpath 所做的更改(通过项目 - 属性 - Java 构建路径)。
    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多