2014-07-01 46 views
0

上午HUDSON ci-server。我有一个要求,说我必须使用Hudson远程访问API并在hudson中创建新作业,或编辑现有作业的配置文件并将其更新为hudson作业config.xml的模式

我有一个样本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>[email protected]</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标签有一些子标签其中是共同的每个进入标签。我想通过Java访问所有这些不同的标签使用Hudosn远程访问API

例如:

<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> 

,所以我想acceess DaysToKeep的numToKeep artifactDaysToKeep等为标签和其它标签存在于config.xml中

其余任何帮助将不胜感激。 在此先感谢。

回答

0

你可以通过获取项目的一个实例,然后通过使用下面的代码

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

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

的配置是字符串生成的配置文件更新项目配置做到这一点。

欲了解更多参考这个职位How to update Jenkins config.xml from Java code?

相关问题