2017-05-24 65 views
0

我想將我的xml文件轉換爲html。 xslt代碼正在工作,但我不知道如何在我的groovy腳本中執行轉換。我想:用xslt在groovy中執行xml到html轉換

import javax.xml.transform.TransformerFactory 
    import javax.xml.transform.stream.StreamResult 
    import javax.xml.transform.stream.StreamSource 
    import javax.xml.transform.Templates 

stage('make Changelog'){ 
    node('fetch'){ 
     dir('C:\\Jenkins_Slave_FETCH\\xsl_changelog') { 

      def xslt= readFile("C:\\Jenkins_Slave_FETCH\\xsl_changelog\\convert.xsl") 

      def transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt))) 

      def xml= readFile("C:\\Jenkins_Slave_FETCH\\xsl_changelog\\tmp.xml") 

      def html = new FileOutputStream("C:\\Jenkins_Slave_FETCH\\xsl_changelog\\Changelog.html") 

      transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(html)) 
     } 
    } 
} 

但是這個代碼導致以下錯誤:

[Pipeline] stage 
[Pipeline] { (make Changelog) 
[Pipeline] node 
Running on DEGTW6216_Slave_FETCH in C:\Jenkins_Slave_FETCH\workspace\Gen7000\test 
[Pipeline] { 
[Pipeline] dir 
Running in C:\Jenkins_Slave_FETCH\xsl_changelog 
[Pipeline] { 
[Pipeline] readFile 
[Pipeline] readFile 
[Pipeline] } 
[Pipeline] // dir 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] End of Pipeline 
an exception which occurred: 
    in field locals 
    in field caller 
    in field e 
    in field program 
    in field thread 
    in field body 
    in field step 
    in field thread 
    in field this$0 
    in field returnAddress 
    in field parent 
    in field caller 
    in field parent 
    in field parent 
    in field capture 
    in field def 
    in field closures 
    in object [email protected] 
Caused: java.io.NotSerializableException: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) 
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65) 
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56) 
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50) 
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179) 
    at java.io.ObjectOutputStream.writeObject(Unknown Source) 
    at java.util.HashMap.internalWriteEntries(Unknown Source) 
    at java.util.HashMap.writeObject(Unknown Source) 
    at sun.reflect.GeneratedMethodAccessor124.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:271) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65) 
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56) 
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50) 
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179) 
    at java.io.ObjectOutputStream.writeObject(Unknown Source) 
    at java.util.HashMap.internalWriteEntries(Unknown Source) 
    at java.util.HashMap.writeObject(Unknown Source) 
    at sun.reflect.GeneratedMethodAccessor124.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:271) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58) 
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111) 
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:140) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:458) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:434) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:422) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:362) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:242) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:230) 
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) 
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Finished: FAILURE 

是否有人知道如何解決這個問題?

+0

更好的編寫/調試任何groovy IDE的正確代碼。除了明顯的例外,它將更容易修復它們。你可以使用groovy二進制的'groovyColsole'。我認爲你提供的錯誤說管道無法顯示你的groovy代碼中發生的錯誤。所以,我們不能解決它,因爲我們沒有看到它) – daggett

回答

1

我解決了這個問題。看起來jenkins的安全設置對於groovy中的xsl轉換並不合適。所以我在PowerShell中執行轉換,一切正常。

0

你可以在這裏找到一些可能的解決方案:https://github.com/jenkinsci/pipeline-plugin/blob/f31f8fda473e4910f344afb50b0c95844825b7c9/TUTORIAL.md#serializing-local-variables

您的解決方案可能會是你的變壓器和HTML變量分拆成另一種功能,並添加註釋@NonCPS,這將確保它不會被序列化。

缺點是你不能在NonCPS函數中調用步驟,所以你必須在主jenkinsfile階段加載你的文件,然後把文件的字符串傳遞給一個函數。

我還沒有真正嘗試過這一點,因爲我沒有你的文件,但功能可能是這樣的:

@NonCPS 
def xmlToHtml(xslt, xml) { 
    def transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt))) 
    def html = new FileOutputStream("C:\\Jenkins_Slave_FETCH\\xsl_changelog\\Changelog.html") 
    transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(html)) 
}