尝试在 Jenkins 上使用 Maven 进行单元测试 groovy 时,测试 class 无法解析主 class

Test class cannot resolve main class when trying to Unit Test groovy using maven on Jenkins

我无法让我的单元测试在 Maven 中为用 Groovy 编写的 Jenkins 共享库工作。

我是 Maven 的新手,也是 Jenkins 的新手。情况如下: 我们有一个 TFVC 服务器托管我们的共享库。共享库是这样存储的:

TFVC
- sharedLibrary
    - src
        - br
            - common
                - v1
                    - SomeClass1.groovy
                    - SomeClass2.groovy
                    - SomeClass3.groovy
        - test
            - groovy
                - SomeClass1Tests.groovy
                - SomeClass2Tests.groovy
- Jenkinsfile
- pom.xml

无法更改结构 src-br-common-v1。我根据网上查到的资料添加了test-groovy结构

Jenkinsfile 包含在 Maven 中测试库的作业。它在呼唤

mvn clean gplus:testCompile

我的 POM 如下所示:

<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>my.company</groupId>
  <artifactId>sharedLib</artifactId>
  <version>1.0</version>
  <name>Jenkins Shared Pipeline Library</name>

  <repositories>
    <repository>
      <id>jenkins</id>
      <url>http://repo.jenkins-ci.org/releases/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>org.jenkins-ci.main</groupId>
      <artifactId>maven-plugin</artifactId>
      <version>3.5</version>
    </dependency>

    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-install-plugin</artifactId>
      <version>2.5.2</version>
    </dependency>

    <dependency>
      <groupId>org.jenkins-ci.main</groupId>
      <artifactId>jenkins-core</artifactId>
      <version>2.85</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.12</version>
    </dependency>

    <dependency>
      <groupId>com.cloudbees</groupId>
      <artifactId>groovy-cps</artifactId>
      <version>1.11</version>
    </dependency>

    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>script-security</artifactId>
      <version>1.24</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency>

    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>pipeline-utility-steps</artifactId>
      <version>1.5.1</version>
    </dependency>

   <dependency>
      <groupId>org.apache.maven.doxia</groupId>
      <artifactId>doxia-site-renderer</artifactId>
      <version>1.9.2</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/br/common/v1</sourceDirectory>
    <testSourceDirectory>src/test/groovy</testSourceDirectory>
    <resources>
      <resource>
        <directory>E:\Jenkins\plugins\pipeline-utility-steps\WEB-INF\lib</directory>
      </resource>
    </resources>
    <plugins>
     <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>addSources</goal>
              <goal>addTestSources</goal>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
        <sources>
          <source>
            <directory>${project.basedir}/src/br/common/v1</directory>
            <includes>
              <include>**/*.groovy</include>
            </includes>
          </source>
        </sources>
        <testSources>
          <testSource>
            <directory>${project.basedir}/src/br/common/v1</directory>
            <directory>${project.basedir}/src/test/groovy</directory>
            <includes>
              <include>**/*.groovy</include>
            </includes>
          </testSource>
        </testSources>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <configuration>
          <trimStackTrace>false</trimStackTrace>
          <argLine>${surefireArgLine}</argLine>
          <includes>
            <include>**/*Test*.*</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

这是测试的简化版本 class,让我们假设 SomeClass1 包含一个方法 returnTrue,它完全符合您的想法:

package test.groovy;
import br.common.v1.SomeClass1;

public class SomeClass1Tests extends GroovyTestCase
{
    public void testReturnTrue() {
        def someClass1Object = new SomeClass1();

        def expected = true;

        def result = someClass1Object.returnTrue();

        assertEquals(expected, result);
    }
}

我现在遇到问题,我的测试 class 无法解析我想测试的 class。

Unable to resolve class br.common.v1.SomeClass1 @ line 2, column 1

最初我的测试文件在 TFVC 的另一个位置,但那没有用,我读到 gmaven-plus 对存储测试的位置非常挑剔 classes.

我希望我以实用的方式提供了所有需要的信息,如果我遗漏了什么,请告诉我。

提前感谢您的帮助!

为 maven 配置的源目录是特定的(它们指向文件所在的位置)。如果要导入 br.common.v1,请确保目录层次结构 br/common/v1 在源根目录中。