如何创建pom.xml来编译我的Jenkins Pipeline共享库?

时间:2019-03-15 20:35:37

标签: maven jenkins jenkins-pipeline

Jenkins Pipeline Shared Library通常具有以下目录结构:

(root)
+- src                     # Groovy source files
|   +- org
|       +- foo
|           +- Bar.groovy  # for org.foo.Bar class
+- vars
|   +- foo.groovy          # for global 'foo' variable
|   +- foo.txt             # help for 'foo' variable
+- resources               # resource files (external libraries only)
|   +- org
|       +- foo
|           +- bar.json    # static helper data for org.foo.Bar

这些是使用Jenkins库中的某些代码的grovvy文件。我希望能够使用maven(可能使用GMavenPlus maven插件)并定义一些Jenkins库作为依赖项来编译它们。

然后我想进行编译,以便在提交或上传到Jenkins之前可以验证文件。在编辑文件时,我可能还会给我更好的代码完成率。

有人可以帮我创建一个可以编译它的pom.xml文件吗?

2 个答案:

答案 0 :(得分:3)

我建议使用Gradle代替Maven。在下面,您可以找到一个最小的build.gradle文件,该文件可让您编译,测试和打包Jenkins共享库(如果需要与其他项目共享,甚至可以安装在Maven存储库中):

apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'jacoco'
apply plugin: 'maven'


repositories {
    mavenCentral()
}

sourceSets {
    main {
        groovy {
            srcDirs = ['src', 'vars']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    test {
        groovy {
            srcDirs = ['test']
        }
    }
}

dependencies {
    compile 'com.cloudbees:groovy-cps:1.22'
    compile 'org.codehaus.groovy:groovy-all:2.4.12'

    // https://github.com/jenkinsci/JenkinsPipelineUnit
    testCompile 'com.lesfurets:jenkins-pipeline-unit:1.1'
    testCompile 'junit:junit:4.12'
}

jacocoTestReport {
    reports {
        xml.enabled true
    }
}

这个最小的build.gradle文件使用jenkins-pipeline-unit-Jenkins管道的单元测试框架。它非常方便,可以使生活轻松10倍。

或者,您可以检查以下有关Jenkins Pipeline共享库的Gradle模板项目-https://github.com/mkobit/jenkins-pipeline-shared-library-example它具有许多其他功能,还有助于维护您的共享库项目。

但是,如果您确实需要使用Maven,则可以使用以下pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>jenkins-shared-library</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.cloudbees</groupId>
            <artifactId>groovy-cps</artifactId>
            <version>1.22</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.lesfurets</groupId>
            <artifactId>jenkins-pipeline-unit</artifactId>
            <version>1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/</sourceDirectory>
        <testSourceDirectory>test/</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.6.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addSources</goal>
                            <goal>addTestSources</goal>
                            <goal>generateStubs</goal>
                            <goal>compile</goal>
                            <goal>generateTestStubs</goal>
                            <goal>compileTests</goal>
                            <goal>removeStubs</goal>
                            <goal>removeTestStubs</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>
                            <directory>${project.basedir}/src</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                        <source>
                            <directory>${project.basedir}/vars</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                    </sources>
                    <testSources>
                        <source>
                            <directory>${project.basedir}/test</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                    </testSources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                            <goal>test-jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这比build.gradle文件的等效物少得多。

答案 1 :(得分:0)

到目前为止,我在Maven中的解决方案可以在以下pom.xml中看到。您可以使用命令mvn clean compile编译源文件。如果您想知道源文件中的内容以及所使用的项目结构,可以查看:https://github.com/snukone/compile-jenkins-pipeline

如果您还有其他疑问或改进想法,请告诉我:)

<?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>org.foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.5.6</version>
            <type>pom</type>
        </dependency>
    </dependencies>

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

    <build>
        <resources>
            <resource>
                <directory>resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src</source>
                                <source>vars</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.9.2-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.5.6-01</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>