2013-03-06 80 views
3

我的目標是讓企業Java應用程序建立在Jenkins上。該應用程序由4個項目(客戶端接口,Webapplcation(包括Faces),EJB應用程序(包括JPA),EAR-Container-Project)組成。Eclipse與Java EE(EJB),ANT和Jenkins/Hudson

當Eclipse將這些項目部署到glassfish服務器時,它將Webapplication(war-file),Client-Interfaces(jar-file)和EJB-Interfaces(jar-file)組裝成一個ear-File。

現在,如果我想要使用持續集成,我需要在CI-Server jenkins上實現相同的功能。

我的第一個想法是用ant解決這個問題,所以我使用了Eclipse的Export-Function併爲項目生成了構建文件。

問題是,生成的Build-Files引用了項目目錄之外的Java EE庫(例如Glassfish-Runtime,JPA-Library等)。大約有30個圖書館。

這意味着我不能在jenkins上使用該文件,因爲這些庫缺失。當然,我可以複製這些,但我不認爲這是應該如何完成的。

那麼,在CI服務器上構建Java EE企業應用程序的最佳方式是什麼?我是否必須自己編寫ANT腳本並將這些庫複製到項目中?或者我錯過了一些明顯的東西?

回答

3

因爲我沒有找到適合自己的東西,所以我寫了一個螞蟻腳本,它自己覆蓋了我的需求。

這裏是我的解決方案,如果這有助於在未來的人: `

<project basedir="." default="build" name="Project"> 

<available property="glassfishdir" value="/opt/glassfish3/glassfish/modules" 
     file="/opt/glassfish3/glassfish/modules" type="dir" /> 

<!-- ########### Property Declarations ################################################################################################################### --> 
<property name="debuglevel" value="source,lines,vars"/> 
<property name="target" value="1.6"/> 
<property name="source" value="1.6"/> 
<property name="builddir" value="build" /> 
<property name="outputartifacts" value="out" /> 

<property name="web.name" value="ProjectWeb" /> 
<property name="web.projectpath" value="ProjectWeb"/> 
<property name="web.src" value="${web.projectpath}/src" /> 
<property name="web.builddir" value="${builddir}/web" /> 
<property name="web.builddir.classes" value="${web.builddir}/WEB-INF/classes"/> 

<property name="ejb.name" value="ProjectEJB" /> 
<property name="ejb.projectpath" value="ProjectEJB"/> 
<property name="ejb.src" value="${ejb.projectpath}/src"/> 
<property name="ejb.builddir" value="${builddir}/ejb" /> 
<property name="ejb.builddir.classes" value="${ejb.builddir}/classes" /> 

<property name="ejbclient.name" value="ProjectEJBClient" /> 
<property name="ejbclient.projectpath" value="ProjectEJBClient"/> 
<property name="ejbclient.src" value="${ejbclient.projectpath}/src"/> 
<property name="ejbclient.builddir" value="${builddir}/ejbclient" /> 
<property name="ejbclient.builddir.classes" value="${ejbclient.builddir}/classes"/> 

<property name="ear.name" value="ProjectApplication" /> 
<property name="ear.dir" value="ProjectEAR" /> 

<!-- ########### Main Targets ################################################################################################################### --> 
<target name="build" depends="create-ear"> 
</target> 

<target name="clean-build"> 
    <antcall target="clean" /> 
    <antcall target="build" /> 
</target> 

<target name="clean"> 
    <delete dir="${builddir}"/> 
    <delete dir="${outputartifacts}"/> 
</target> 

<target name="init"> 
    <mkdir dir="${outputartifacts}" /> 
</target> 

<!-- ########### EJB App ################################################################################################################### --> 
<target name="init-ejb" depends="init"> 
    <mkdir dir="${ejb.builddir}" /> 
    <copy includeemptydirs="false" todir="${ejb.builddir.classes}"> 
     <fileset dir="${ejb.src}"> 
      <exclude name="**/*.java"/> 
     </fileset> 
    </copy> 
</target> 
<target name="build-ejb" depends="init-ejb"> 
    <javac debug="true" debuglevel="${debuglevel}" destdir="${ejb.builddir.classes}" includeantruntime="false" source="${source}" target="${target}"> 
     <src path="${ejb.src}"/> 
     <classpath> 
      <fileset dir="${glassfishdir}"> 
       <include name="**/*.jar"/> 
      </fileset> 

      <fileset dir="${outputartifacts}"> 
       <include name="**/*.jar"/> 
      </fileset>     
     </classpath> 
    </javac> 
</target> 

<!-- ########### WEB ################################################################################################################### --> 
<target name="init-web" depends="init"> 
    <mkdir dir="${web.builddir.classes}"/> 
    <copy includeemptydirs="false" todir="${web.builddir}"> 
     <fileset dir="${web.projectpath}/WebContent"> 
     </fileset> 
    </copy> 
    <copy includeemptydirs="false" todir="${web.builddir.classes}"> 
     <fileset dir="${web.src}"> 
      <exclude name="**/*.java"/> 
     </fileset> 
    </copy> 
</target> 

<target depends="init-web,create-ejb-client" name="build-web"> 
    <javac debug="true" debuglevel="${debuglevel}" destdir="${web.builddir.classes}" includeantruntime="false" source="${source}" target="${target}"> 
     <src path="${web.src}"/> 
     <classpath> 
      <fileset dir="${glassfishdir}"> 
       <include name="**/*.jar"/> 
      </fileset> 
      <fileset dir="out/"> 
       <include name="**/*.jar"/> 
      </fileset> 
     </classpath> 
    </javac> 
</target> 
<!-- ############## EJB CLIENT ################################################################################################################ --> 
<target name="init-ejb-client" depends="init"> 
    <mkdir dir="${ejbclient.builddir}"/> 
    <copy includeemptydirs="false" todir="${ejbclient.builddir.classes}"> 
     <fileset dir="${ejbclient.src}"> 
      <exclude name="**/*.java"/> 
     </fileset> 
    </copy> 
</target> 


<target depends="init-ejb-client" name="build-ejb-client"> 
    <javac debug="true" debuglevel="${debuglevel}" destdir="${ejbclient.builddir.classes}" includeantruntime="false" source="${source}" target="${target}"> 
     <src path="${ejbclient.src}"/> 
     <classpath> 
      <fileset dir="${glassfishdir}"> 
       <include name="**/*.jar"/> 
      </fileset>   
     </classpath> 
    </javac> 
</target> 


<!-- ############ CREATE ARCHIVES################################################################################################################## --> 
<target name="create-web" depends="build-web"> 
    <war destfile="${outputartifacts}/${web.name}.war" basedir="${web.builddir}" webxml="${web.projectpath}/WebContent/WEB-INF/web.xml"/> 
</target> 

<target name="create-ejb-client" depends="build-ejb-client"> 
    <jar destfile="${outputartifacts}/${ejbclient.name}.jar" basedir="${ejbclient.builddir.classes}" includes="**/*"/> 
</target> 

<target name="create-ejb" depends="build-ejb"> 
    <jar destfile="${outputartifacts}/${ejb.name}.jar" basedir="${ejb.builddir.classes}" includes="**/*"> 
     <manifest> 
      <attribute name="Class-Path" value="${ejbclient.name}.jar"/> 
     </manifest> 
    </jar> 
</target> 

<target name="create-ear" depends="create-ejb-client,create-web,create-ejb"> 
    <ear destfile="${outputartifacts}/${ear.name}.ear" appxml="${ear.dir}/EarContent/META-INF/application.xml"> 
     <fileset dir="${outputartifacts}" includes="*.jar,*.war"/> 
    </ear> 
</target> 


</project> 

`

+0

這是一個非常好的螞蟻文件構建,你可以輕鬆地與jenkins集成,但如何運行測試? 您可以嘗試使用嵌入式應用程序服務器或嘗試將測試作爲外部應用程序運行。 – 2014-10-31 10:47:39

0

使用Maven。
Maven允許在單個xml文件(pom)中定義所有依賴項,依賴項將在編譯階段自動從互聯網上下載。
Maven附帶一組插件,以促進持續集成,如能夠啓動容器,運行測試並自動關閉它。 Maven與jenkins本地整合。
Maven定義了一個複雜的生命週期,專爲這類問題而設計,允許編譯,運行單元測試,打包,運行集成測試並使用由jenkins觸發的單個命令進行部署;

Maven絕對是這裏的解決方案。

+0

請注意,如果4個不同項目相互依賴,則必須將它們打包在引用所有子項目的共同父項目中。 (多模塊maven項目)。您可能需要安裝和配置nexus服務器,以允許將自己的構件(包括項目打包的所有jar,war,ear)暴露給maven依賴機制(只有通用的框架構件在maven公共存儲庫上公開) – Gab 2013-03-06 14:33:29

+0

雖然我得出的結論是maven會是更好的解決方案,但由於我們大學的開發環境,我無法切換到maven。 – Manuel 2013-04-19 17:01:51

0

您還可以使用「導出自動創建在Eclipse中的build.xml ...>常規\ Ant Buildfile「。通過這種方式,正確的類路徑將生成到項目中已有的JAR中。

如果項目之間存在依賴關係,你只需要配置一個構建文件在Jenkins上運行,因爲它會自動從其他項目調用構建文件。