如何让Eclipse将文件夹识别为"代码文件夹"什么时候导入项目?

时间:2016-01-14 11:37:49

标签: java eclipse maven

我有一个Maven Java项目,我在其中添加了pom:

<build>
....
    <plugin>
                <!-- adding second test source directory (just for integration tests) -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${plugin.build-helper-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>add-integration-test-source</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/integration-test/java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-integration-test-resource</id>
                        <phase>generate-test-resources</phase>
                        <goals>
                            <goal>add-test-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/integration-test/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
</build>

InteliJ将我在兼容性测试下的java和资源文件夹识别为代码文件夹,但Eclipse并没有。 有没有办法在导入项目时将这些文件夹添加为代码文件夹?

2 个答案:

答案 0 :(得分:0)

尝试右键单击上下文菜单中Project Explorer选择Build Path选项中的文件夹,然后点击选择Use as Source Folder后出现的菜单中的Build Path

答案 1 :(得分:0)

我建议不要在Maven中使用自己的目录布局,因为这会导致很多问题,而且你总是要围绕它进行配置。 Just stick to the standard.

  • 单独的集成测试和单元测试不是通过其源文件夹,而是通过其名称。
  • 将所有测试都放在src/test/java中。此时您不必配置任何内容,默认情况下会采用此路径。
  • 调用集成测试IT*.java和单元测试UT*.java
  • 它们可以单独运行,因为maven-surefire-plugin执行单元测试并且maven-failsafe-plugin执行了集成测试。您可以定义用于标识测试类的文件名模式。
  • 您还可以创建仅运行UT或仅运行IT的配置文件。

    <project>
    <!-- ... -->
    <build>
    <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.13</version>
      <configuration>
        <includes>
          <include>**/UT*.java</include>
        </includes>
        <excludes>
          <exclude>**/IT*.java</exclude>
        </excludes>
      </configuration>
    </plugin>
    
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>2.18</version>
      <configuration>
        <includes>
          <include>**/IT*.java</include>
        </includes>
      </configuration>
    
      <executions>
        <execution>
          <id>failsafe-integration-tests</id>
          <phase>integration-test</phase>
          <goals>
            <goal>integration-test</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

进一步阅读:http://tomaszdziurko.pl/2013/01/running-unit-tests-integration-tests-separately-maven-testng/

还有一篇关于正确使用集成测试的文章:http://zeroturnaround.com/rebellabs/the-correct-way-to-use-integration-tests-in-your-build-process/