2

我的公司只使用 Ant 来构建项目。但是,Jenkins 只建议将 Maven作为插件开发的构建工具。

如何使用 Ant 将我的 Jenkins 插件打包到一个.hpi文件中并不惜一切代价避免使用 Maven?

4

2 回答 2

1

这是一种使用 Ant 构建 Jenkins 插件的方法。让我们编写一个脚本来构建一个名为“awesome”的插件骨架。

默认插件树状结构

awesome-plugin/
-- awesome/
   -- src/
   -- pom.xml

指示

  1. 添加一个lib/包含以下 jar 的文件夹:
    • 可以在您的 Jenkins 主目录中找到Jenkins\war\WEB-INF\lib注意:您必须使用与当前 Jenkins 使用的完全相同的版本):
      • 访问修饰符注释 1.4.jar
      • 桥接方法注释 1.4.jar
      • commons-io-1.4.jar
      • 番石榴-11.0.1.jar
      • jenkins-core-1.513.jar
      • json-lib-2.4-jenkins-1.jar
      • 远程处理-2.23.jar
      • sezpoz-1.9.jar
      • 订书机-1.207.jar
    • 可在网上找到(您可以选择最后发布的版本):
  2. 将现有的替换pom.xml为以下build.xml.
  3. 在 Ant 脚本中,您应该适应:
    • 项目名称,awesome在这里,
    • 插件版本,
    • 这个插件的 Jenkins 版本,
    • 项目 group.id (主包),org.jenkinsci.plugins.awesome在这里。

新插件树状结构

awesome-plugin/
-- awesome/
   -- src/
   -- lib/
      -- you should have 10 jars here
   -- build.xml

构建.xml

<!-- Project dependent properties -->
<property name="project_name" value="awesome"/>
<property name="project_version" value="1.0"/>
<property name="jenkins_version" value="1.513"/> <!-- which version of Jenkins is this plugin built against? -->
<property name="project_groupid" value="org.jenkinsci.plugins.awesome"/>

<!-- Build properties -->
<property name="lib_dir" value="./lib"/>
<property name="bin_dir" value="./bin" />
<property name="target_dir" value="./target"/>
<property name="target_bin_dir" value="${target_dir}/${project_name}"/>
<property name="plugin_targetMetaInf_dir" value="${target_bin_dir}/META-INF"/>
<property name="plugin_targetWebInf_dir" value="${target_bin_dir}/WEB-INF"/>
<property name="plugin_targetWebInfBin_dir" value="${plugin_targetWebInf_dir}/classes"/>

<!-- Project paths -->
<path id="project.source.path">
    <pathelement path="src/main/java" />
</path>
<path id="project.class.path">
    <fileset dir="${lib_dir}" includes="*.jar"/>    
</path>

<!-- Build flow -->
<target name="build">
    <antcall target="clean" />
    <antcall target="compile" />
    <antcall target="createTreeDirectory" />
    <antcall target="copyBin"/>
    <condition property="has_file">
        <and>
            <available file="${target_dir}/${project_name}.hpi" type="file"/>
        </and>
    </condition>
    <antcall target="createHpi"/>
    <condition property="has_dir">
        <and>
            <available file="${target_bin_dir}" type="dir"/>
        </and>
    </condition>
    <antcall target="cleanTargetDirectory" />
</target>

<!-- Cleans existing binaries -->
<target name="clean">
    <delete includeEmptyDirs="true" quiet="true">
        <fileset dir="${bin_dir}" />
    </delete>
    <mkdir dir="${bin_dir}"/>
</target>

<!-- Compiles JAVA code -->
<target name="compile">
    <javac includeantruntime="false" destdir="${bin_dir}" debug="false" optimize="${optimize}" deprecation="${deprecation}" classpathref="project.class.path">
        <src refid="project.source.path" />
    </javac>
</target>

<!-- Creates necessary target folders -->
<target name="createTreeDirectory" >
    <mkdir dir="${target_bin_dir}"/>
    <mkdir dir="${plugin_targetMetaInf_dir}"/>
    <mkdir dir="${plugin_targetWebInf_dir}"/>
    <mkdir dir="${plugin_targetWebInfBin_dir}"/>
</target>

<!-- Moves new binaries to the plugin target -->
<target name="copyBin">
    <copy todir="${plugin_targetWebInfBin_dir}" >
        <fileset dir="${bin_dir}"/>
        <fileset dir="src/main/resources"/>
    </copy>
</target>

<!-- Cleans the target directory -->
<target name="cleanTargetDirectory" if="has_dir">
    <delete dir="${target_bin_dir}"/>
</target>

<!-- Backup previous plugin -->
<target name="saveOldHpiFile" if="has_file">
    <move file="${target_dir}/${project_name}.hpi" tofile="${target_dir}/${project_name}.save.hpi"/>
</target>

<!-- Archives the plugin -->
<target name="createHpi">
    <antcall target="saveOldHpiFile"/>
    <jar destfile="${target_dir}/${project_name}.hpi" basedir="${target_bin_dir}">
        <manifest>
            <attribute name="Manifest-Version" value="{project_version}"/>
            <attribute name="Built-By" value="${user.name}"/>
            <attribute name="Created-By" value="${user.name}"/>
            <attribute name="Build-Jdk" value="${ant.java.version}"/>
            <attribute name="Extension-Name" value="${project_name}"/>
            <attribute name="Implementation-Title" value="${project_name}"/>
            <attribute name="Implementation-Version" value="${version}"/>
            <attribute name="Group-Id" value="${project_groupid}"/>
            <attribute name="Short-Name" value="${project_name}"/>
            <attribute name="Long-Name" value="${project_name}"/>
            <attribute name="Plugin-Version" value="${project_version}"/>
            <attribute name="Jenkins-Version" value="${jenkins_version}"/>
            <attribute name="Hudson-Version" value="${jenkins_version}"/>
        </manifest>
    </jar>
</target>

要启动构建,cd请向build.xml和类型ant

于 2013-07-26T11:25:11.740 回答
0

我知道您说“不惜一切代价”,但妥协可能会减少工作量,并且仍然可以提供超快速的构建。我尽量避免使用 maven 的一个重要原因是编译时间是 sloooowwwwww。也就是说,maven 非常擅长创建 hpl 文件和处理依赖关系。以下目标对于帮助建立超快速的非 Maven 构建非常有用:

  • 使用 'mvn hpi:hpl' 生成 hpl 文件
  • 使用 'mvn dependency:copy-dependencies' 下载所有依赖项,并将它们放入目标/依赖项中,在那里很容易从您的 ant 脚本中引用它们(如果需要,您可以添加一个符号链接,从 lib 到目标/依赖项)
于 2013-07-26T16:55:32.367 回答