hudson job config.xml的模式

时间:2014-07-01 11:28:32

标签: java xml hudson hudson-api

HUDSON ci-server 的新手。我有一个要求,声明我必须使用Hudson远程访问API并在hudson中创建新作业或编辑现有作业的配置文件并更新它

我有一个示例 config.xml 文件。

<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>Free style Basic sample Job for POC . will create this job with all the required fields . then will take this config.xml and will try to validate and study this schema .</description>
<project-properties class="java.util.concurrent.ConcurrentHashMap">
<entry>
  <string>hudson-plugins-disk_usage-DiskUsageProperty</string>
  <base-property>
    <originalValue class="hudson.plugins.disk_usage.DiskUsageProperty"/>
    <propertyOverridden>false</propertyOverridden>
  </base-property>
</entry>
<entry>
  <string>logRotator</string>
  <log-rotator-property>
    <originalValue class="hudson.tasks.LogRotator">
      <daysToKeep>5</daysToKeep>
      <numToKeep>-1</numToKeep>
      <artifactDaysToKeep>-1</artifactDaysToKeep>
      <artifactNumToKeep>-1</artifactNumToKeep>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
  </log-rotator-property>
</entry>
<entry>
  <string>scmCheckoutRetryCount</string>
  <integer-property>
    <originalValue class="int">5</originalValue>
    <propertyOverridden>false</propertyOverridden>
  </integer-property>
</entry>
<entry>
  <string>hudson-tasks-Mailer</string>
  <external-property>
    <originalValue class="hudson.tasks.Mailer">
      <recipients>abc@example.com</recipients>
      <dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild>
      <sendToIndividuals>false</sendToIndividuals>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
    <modified>true</modified>
  </external-property>
</entry>
<entry>
  <string>hudson-plugins-build_timeout-BuildTimeoutWrapper</string>
  <external-property>
    <originalValue class="hudson.plugins.build_timeout.BuildTimeoutWrapper">
      <timeoutMinutes>5</timeoutMinutes>
      <failBuild>false</failBuild>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
    <modified>true</modified>
  </external-property>
</entry>
<entry>
  <string>builders</string>
  <describable-list-property>
    <originalValue class="hudson.util.DescribableList">
      <hudson.tasks.Ant>
        <targets></targets>
        <antName>org.apache.ant_1.7.1</antName>
      </hudson.tasks.Ant>
      <hudson.tasks.Shell>
        <command>HUDSON_USER</command>
      </hudson.tasks.Shell>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
  </describable-list-property>
</entry>
<entry>
  <string>hudson-triggers-SCMTrigger</string>
  <trigger-property>
    <originalValue class="hudson.triggers.SCMTrigger">
      <spec>@daily</spec>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
  </trigger-property>
</entry>
<entry>
  <string>jdk</string>
  <string-property>
    <originalValue class="string">(Inherit From Job)</originalValue>
    <propertyOverridden>false</propertyOverridden>
  </string-property>
</entry>
<entry>
  <string>hudson-tasks-ArtifactArchiver</string>
  <external-property>
    <originalValue class="hudson.tasks.ArtifactArchiver">
      <compressionType>GZIP</compressionType>
      <latestOnly>false</latestOnly>
      <autoValidateFileMask>false</autoValidateFileMask>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
    <modified>true</modified>
  </external-property>
</entry>
<entry>
  <string>scm</string>
  <scm-property>
    <originalValue class="hudson.scm.NullSCM"/>
    <propertyOverridden>false</propertyOverridden>
  </scm-property>
</entry>
<entry>
  <string>hudson-plugins-release-ReleaseWrapper</string>
  <external-property>
    <originalValue class="hudson.plugins.release.ReleaseWrapper">
      <releaseVersionTemplate></releaseVersionTemplate>
      <doNotKeepLog>false</doNotKeepLog>
      <overrideBuildParameters>false</overrideBuildParameters>
      <parameterDefinitions/>
      <preBuildSteps/>
      <postBuildSteps/>
      <postSuccessfulBuildSteps/>
      <postFailedBuildSteps/>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
    <modified>true</modified>
  </external-property>
</entry>
</project-properties>
 <keepDependencies>false</keepDependencies>
<creationTime>1403865219075</creationTime>
 <properties/>
     <cascadingChildrenNames class="java.util.concurrent.CopyOnWriteArraySet"/>
     <cascading-job-properties class="java.util.concurrent.CopyOnWriteArraySet">
    <string>hudson-plugins-batch_task-BatchTaskProperty</string>
  <string>hudson-plugins-disk_usage-DiskUsageProperty</string>
  <string>hudson-plugins-jira-JiraProjectProperty</string>
  <string>org-hudsonci-plugins-snapshotmonitor-WatchedDependenciesProperty</string>
  <string>hudson-plugins-promoted_builds-JobPropertyImpl</string>
   </cascading-job-properties>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>false</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <concurrentBuild>false</concurrentBuild>
  <cleanWorkspaceRequired>false</cleanWorkspaceRequired>
 </project>

现在我有一个名为project-properties的标签。在其中我有多个条目标记。每个 enrty 标记都有一些子标记,其中字符串对每个条目都是通用的 tag。我想使用 Hudosn远程访问API 通过java访问所有这些不同的标签。

例如:

<entry>
      <string>logRotator</string>
      <log-rotator-property>
      <originalValue class="hudson.tasks.LogRotator">
      <daysToKeep>5</daysToKeep>
      <numToKeep>-1</numToKeep>
      <artifactDaysToKeep>-1</artifactDaysToKeep>
      <artifactNumToKeep>-1</artifactNumToKeep>
    </originalValue>
    <propertyOverridden>false</propertyOverridden>
  </log-rotator-property>
</entry>

所以我想加入 daysToKeep numToKeep artifactDaysToKeep 等等 config.xml

中存在的其他标签和其他标签

任何帮助将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以通过获取Item的实例然后使用以下代码更新项目配置来完成此操作

Source streamSource = new StreamSource(new StringReader(config));
    try {
        proj.updateByXml(streamSource);

    } catch (IOException ex) {
        Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

config是字符串生成的配置文件。

有关详细信息,请参阅此帖How to update Jenkins config.xml from Java code?