【问题标题】:How do I create a pom.xml to compile my Jenkins Pipeline shared libs?如何创建 pom.xml 来编译我的 Jenkins Pipeline 共享库?
【发布时间】:2019-03-15 20:35:36
【问题描述】:

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 文件吗?

【问题讨论】:

  • 你能说明为什么要编译它们吗?
  • @snukone 我编辑了问题来解释。
  • 啊好的,好主意!我从来没有想过。您对@szymon stepniaks 解决方案有何看法?与 maven 相比,Gradle 也是一个不错的构建工具。如果你真的需要一个 mvn 例子,我明天试试。

标签: maven jenkins jenkins-pipeline


【解决方案1】:

我建议使用 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 文件。

【讨论】:

    【解决方案2】:

    到目前为止,我使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2018-02-27
      • 2020-04-25
      • 2019-02-23
      相关资源
      最近更新 更多