Maven in eclipse practices
Upcoming SlideShare
Loading in...5
×
 

Maven in eclipse practices

on

  • 735 views

 

Statistics

Views

Total Views
735
Views on SlideShare
735
Embed Views
0

Actions

Likes
2
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Maven in eclipse practices Maven in eclipse practices Presentation Transcript

    • Apache Maven 與 Eclipse 整合開發實務研討 中華電信研究所 黃培棠 beta@cht.com.tw 2011/07/01
    • 2
    • Eclipse Community Survey 2011 W ha t b uild a nd re le a s e ma na g e me nt p ro d uc ts d o y o u ty p ic a lly us e ? (S e le c t a ll tha t a p p ly .) R e s p o ns e R e s p o ns e A ns we r Op tio ns P e rc e nt Co unt Ant 48.2% 301 Atlassian Bamboo 3.0% 19 Buckminster 3.5% 22 CruiseControl 4.6% 29 Hudson/Jenkins 32.2% 201 IBM Build Forge 0.8% 5 Make 16.0% 100 Maven 30.8% 192 Microsoft Team Foundation Server (TFS) 0.8% 5 PDE Build 16.0% 100 Custom/in-house 11.1% 69 None - I dont use a build and release management 11.5% 72 Dont know 2.2% 14 Other (specify) 8.7% 54 a ns we re d q ue s tio n 624 s k ip p e d q ue s tio n 0http://www.eclipse.org/org/press-release/20110610_survey.php Question 21 3
    • Sonatype Software Development Infrastructure Surveyhttp://go.sonatype.com/content/winter2011surveyresults?elq=cc593c0874f44285ba5f61fc9513b5b3 4
    • http://www.eclipse.org/downloads/compare.php 5Java EE w/ Maven: http://www.sonatype.com/people/category/m2eclipse/
    • BUILD. 6
    • 其實就只有兩件事…• 編譯 (*.java → *.class) – 把 Java 原始碼編譯成可以被 JVM 載入執行的 Byte Code。 – 使用 javac 指令。• 打包 (*.class → jar/war/ear/rar…) – 產生 Deliverable 或是 Deployable 的檔案。 – 使用 jar 指令。 7
    • 透過 IDE 建置、打包• 簡單到不行• 可惜同步大家的 IDE 設定難如登天• 較難自動化執行 8
    • Apache Ant• 什麼設定都可以自己來 “Ant is extremely flexible and does not impose coding conventions or directory layouts to the Java projects which adopt it as a build tool.” http://ant.apache.org/• 可惜 Build File 幾乎是唯寫的 https://bitbucket.org/satyagraha.1956/ant_vis/wiki/Home 9
    • Top 15 Ant Best Practices 1. Adopt Consistent Style Conventions 2. Put build.xml in the Project Root Directory 3. Prefer a Single Buildfile 4. Provide Good Help 5. Provide a Clean Target 6. Manage Dependencies Using Ant 7. Define and Reuse Paths 8. Define Proper Target Dependencies …http://onjava.com/pub/a/onjava/2003/12/17/ant_bestpractices.Html 10
    • Convention over Configuration以簡御繁,綱舉目張 11
    • Maven 哲學• Maven 試著要… – 將專案的 Build 結構套上 Pattern。 – 提供清晰明確的方式使用 Best Practice。 – 讓大家更容易理解專案,進而提升生產力。• Convention over Configuration 12
    • 除了 Build 之外…• Maven 野心不小,還包括… – 文件 – 報告 – 相依性管理 – 版本管理 – 發行 – 散佈 13
    • Maven 程式結構• Maven 主程式 – 只包含最基本的設定及程式執行框架。 – 大部分的功能都是透過外掛程式來達成。• 外掛程式 – 也是 Maven 的 Artifact。 – 提供 Goal 供執行,例如 compiler:compile。 – Goal 是由 Mojo 進行實作。 14
    • Maven 設定 • 設定內容 – 專案間一體適用的資訊。 – 不適合公開的資訊。 • 設定檔路徑 – 全域設定 • $M2_HOME/conf/settings.xml – 使用者設定 • ${user.home}/.m2/settings.xmlhttp://maven.apache.org/settings.html 15
    • POM (Project Object Model)<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cht.demo</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies></project> 16
    • Effective POM (1/2)<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 ... http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" <plugin> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <artifactId>maven-assembly-plugin</artifactId> <modelVersion>4.0.0</modelVersion> <version>2.2-beta-5</version> <groupId>com.cht.demo</groupId> </plugin> <artifactId>demo</artifactId> <plugin> <version>1.0-SNAPSHOT</version> <artifactId>maven-dependency-plugin</artifactId> <dependencies> <version>2.1</version> <dependency> </plugin> <groupId>junit</groupId> <plugin> <artifactId>junit</artifactId> <artifactId>maven-release-plugin</artifactId> <version>4.8.2</version> <version>2.0</version> <scope>test</scope> </plugin> </dependency> </plugins> </dependencies> </pluginManagement> <repositories> <plugins> <repository> <plugin> <snapshots> <artifactId>maven-clean-plugin</artifactId> <enabled>false</enabled> <version>2.4.1</version> </snapshots> <executions> <id>central</id> <execution> <name>Maven Repository Switchboard</name> <id>default-clean</id> <url>http://repo1.maven.org/maven2</url> <phase>clean</phase> </repository> <goals> </repositories> <goal>clean</goal> <pluginRepositories> </goals> <pluginRepository> </execution> <releases> </executions> <updatePolicy>never</updatePolicy> </plugin> </releases> <plugin> <snapshots> <artifactId>maven-surefire-plugin</artifactId> <enabled>false</enabled> <version>2.7.2</version> </snapshots> <executions> <id>central</id> <execution> <name>Maven Plugin Repository</name> <id>default-test</id> <url>http://repo1.maven.org/maven2</url> <phase>test</phase> </pluginRepository> <goals> </pluginRepositories> <goal>test</goal> <build> </goals> <sourceDirectory>D:workspacedemosrcmainjava</sourceDirectory> </execution> <scriptSourceDirectory>D:workspacedemosrcmainscripts</scriptSourceDirectory> </executions> <testSourceDirectory>D:workspacedemosrctestjava</testSourceDirectory> </plugin> <outputDirectory>D:workspacedemotargetclasses</outputDirectory> <plugin> <testOutputDirectory>D:workspacedemotargettest-classes</testOutputDirectory> <artifactId>maven-compiler-plugin</artifactId> <resources> <version>2.3.2</version> <resource> <executions> <directory>D:workspacedemosrcmainresources</directory> <execution> </resource> <id>default-compile</id> </resources> <phase>compile</phase> <testResources> <goals> <testResource> <goal>compile</goal> <directory>D:workspacedemosrctestresources</directory> </goals> </testResource> </execution> </testResources> <execution> <directory>D:workspacedemotarget</directory> <id>default-testCompile</id> <finalName>simple-project-1.0-SNAPSHOT</finalName> <phase>test-compile</phase> <pluginManagement> <goals> <plugins> <goal>testCompile</goal> <plugin> </goals> <artifactId>maven-antrun-plugin</artifactId> </execution> <version>1.3</version> </executions> </plugin> </plugin> ... ... 17
    • Effective POM (2/2)... ...<plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <artifactId>maven-site-plugin</artifactId> <version>2.3.1</version> <version>2.0.1</version> <executions> <executions> <execution> <execution> <id>default-jar</id> <id>default-site</id> <phase>package</phase> <phase>site</phase> <goals> <goals> <goal>jar</goal> <goal>site</goal> </goals> </goals> </execution> <configuration> </executions> <outputDirectory>D:workspacedemotargetsite</outputDirectory></plugin> <reportPlugins><plugin> <reportPlugin> <artifactId>maven-deploy-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId> <version>2.5</version> <artifactId>maven-project-info-reports-plugin</artifactId> <executions> </reportPlugin> <execution> </reportPlugins> <id>default-deploy</id> </configuration> <phase>deploy</phase> </execution> <goals> <execution> <goal>deploy</goal> <id>default-deploy</id> </goals> <phase>site-deploy</phase> </execution> <goals> </executions> <goal>deploy</goal></plugin> </goals><plugin> <configuration> <artifactId>maven-resources-plugin</artifactId> <outputDirectory>D:workspacedemotargetsite</outputDirectory> <version>2.4.3</version> <reportPlugins> <executions> <reportPlugin> <execution> <groupId>org.apache.maven.plugins</groupId> <id>default-resources</id> <artifactId>maven-project-info-reports-plugin</artifactId> <phase>process-resources</phase> </reportPlugin> <goals> </reportPlugins> <goal>resources</goal> </configuration> </goals> </execution> </execution> </executions> <execution> <configuration> <id>default-testResources</id> <outputDirectory>D:workspacedemotargetsite</outputDirectory> <phase>process-test-resources</phase> <reportPlugins> <goals> <reportPlugin> <goal>testResources</goal> <groupId>org.apache.maven.plugins</groupId> </goals> <artifactId>maven-project-info-reports-plugin</artifactId> </execution> </reportPlugin> </executions> </reportPlugins></plugin> </configuration><plugin> </plugin> <artifactId>maven-install-plugin</artifactId> </plugins> <version>2.3.1</version> </build> <executions> <reporting> <execution> <outputDirectory>D:workspacedemotargetsite</outputDirectory> <id>default-install</id> </reporting> <phase>install</phase> </project> <goals> <goal>install</goal> </goals> </execution> </executions></plugin>... 18
    • POM 繼承• Maven 內部定義了 Super POM – 所有的 POM 都自動繼承 Super POM• 可以指定 parent 繼承其他 POM 設定<project xmlns=http://maven.apache.org/POM/4.0.0 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.cht.demo</groupId> <artifactId>demo-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>demo</artifactId></project> 19
    • Maven Build Lifecycle • 多個階段 (Phase) 組成生命週期 (Lifecycle) 階段 說明 validate 檢查專案資訊是否正確。 compile 編譯原始碼。 (單元測試程式碼是在另一個階段: test-compile 裡做的) test 使用合適的單元測試框架測試編譯後的原始碼。 package 依照指定的格式 (預設是 jar, 也可以指定 war, ear 等) 打包 編譯出的檔案,不包含單元測試程式碼。 integration-test 將打包檔部署到整合測試環境進行測試。 verify 驗證測試及分析結果是否符合品質要求 install 安裝到本機檔案庫,供其他專案使用。 deploy 複製檔案到遠端檔案庫,供其他使用者與其他專案使用。http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html 20
    • Maven Dependency 機制• Transitive Dependency 21
    • Maven Dependency Scope • 相依的函式庫 Scope 千萬不要搞錯 Scope 類型 說明 compile 預設值,整個專案的生命週期都會將這個函式庫加入到 classpath 中,包含測試與打包 (輸出成 WAR 或 EAR)。 provided 代表 JRE 或是 Container 等執行環境將提供這個函式庫。 例如開發 Web 專案的時候就應該將 Servlet API 設成 provided,因為 Web Server 提供了這個函式庫。 runtime 執行或測試時需要使用,但是編譯時用不到的函式庫。 test 只有編譯及執行測試程式時才用得到的函式庫,例如 JUint。 (m2eclipse 在執行 src/main/java 下的程式時會自動將這一類的函式 庫排除) system 跟 provided 差不多,不過必需要手動指定函式庫的路徑,所以別的 環境可能無法正常編譯,應該盡可能使用 Repository 上的函式庫。http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html 22
    • Maven Dependency 管理• 版本衝突 – Maven 使用 “nearest definition” 進行版本解析。• 善用 <exclusions> 排除不必要的函式庫。 – 例如將 commons-logging 排除,以 jcl-over-slf4j 代替。• 必要時指定 <optional> – 不要強迫使用者加入所有你使用到的函式庫。 23
    • Maven Remote Repository • 存放函式庫的大倉庫 – Artifact 們都依 G, A, V 參數分門別類放好。 – 分成 RELEASE 跟 SNAPSHOT 兩類。 – Maven 預設使用 Maven Central Repository • http://repo1.maven.org/maven2/ • Sonatype Nexus – 目前最熱門的 Maven Repository Manager 軟體。http://maven.apache.org/guides/introduction/introduction-to-repositories.html 24
    • Maven Local Repository • Artifact 的本地端快取 – 避免做什麼事情都要透過網路連線。 – install 的目的地。 • 預設路徑 – ${user.home}/.m2/repository – 或在 settings.xml 裡透過 <localRepository> 設定路徑。http://maven.apache.org/guides/introduction/introduction-to-repositories.html 25
    • Go Fighting! 百無一用是書生http://zh.wikipedia.org/wiki/File:Normandy7.jpg 26
    • Eclipse Maven 整合• m2e – http://eclipse.org/m2e/• 安裝 m2e – Update Site • http://download.eclipse.org/technology/m2e/milestones/1.0• 安裝 m2eclipse-wtp – Update Site • https://repository.sonatype.org/content/sites/forge-sites/m2eclipse-wtp/0.13.0/S/ 27
    • 設定下載 Source 及 Javadoc 28
    • 設定 Repository 索引 29
    • Keep Things DRY先處戰地而待敵者佚 30
    • DRY• Dont Repeat Yourself (DRY) – 嘴巴毒一點的人說: Duplication is Evil (DIE)• 大師說: “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” 31
    • XML Schema 編譯成 JAXB 2 物件<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.4</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <schemaIncludes> <include>META-INF/resources/sys-domain.xsd</include> </schemaIncludes> <generatePackage>com.sys.domain</generatePackage> <args> <arg>-no-header</arg> </args> </configuration> </execution> </executions></plugin> 32
    • JAX-WS 介面產生 WSDL<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>1.12</version> <executions> <execution> <goals> <goal>wsgen</goal> </goals> <configuration> <sei>com.cht.sys.DemoWebService</sei> <!-- 實際在 Server 裡運作時並不需要 WSDL --> <genWsdl>true</genWsdl> </configuration> </execution> </executions></plugin> 33
    • 編譯 JasperReports 報表檔 (1/2)<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jasperreports-maven-plugin</artifactId> <version>1.0-beta-2</version> <executions> <execution> <goals> <goal>compile-reports</goal> </goals> <configuration> <compiler> net.sf.jasperreports.compilers.JRGroovyCompiler </compiler> <!-- 統一放到將來 jar 的 META-INFjasperreports 目錄下 --> <outputDirectory> ${project.build.outputDirectory}META-INFjasperreports </outputDirectory> </configration> </execution> </executions> ... 34
    • 編譯 JasperReports 報表檔 (2/2) ... <dependencies> <dependency> <!-- 宣告 JasperReports 函式庫,才能載到正確版本的 XML 驗證資料 --> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>4.0.1</version> </dependency> <dependency> <!-- 我們 Report 裡 Expression 的語法預設都是使用 Groovy --> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>1.7.5</version> </dependency> <dependency> <!-- 如果不加這個 Depdendency,這個古董外掛程式就會跑不起來 --> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies></plugin> 35
    • WET • We Edit Terribly, Tumultuously, Tempestuously, Tenaciously, Too much, Timidly, Tortuously, Terrifiedly... • We Enjoy Typing! • We also Enjoy Troubleshooting!http://c2.com/cgi/wiki?DontRepeatYourself 36
    • Handy Eclipse Tips & Plugins工欲善其事,必先利其器 37
    • “Navigator” View• 很懷念檔案總管? 38
    • Eclipse Runner 外掛程式• 你一天按幾次 “Run As…” ?• 官方網站: http://code.google.com/p/eclipserunnerplugin/• Update Site : http://eclipserunnerplugin.googlecode.com/svn/trunk/EclipseRunnerSite/ 39
    • EasyShell 外掛程式• 你一天開幾次檔案總管找專案裡的檔案?• 官方網站: http://pluginbox.sourceforge.net/plugins.html• Update Site : http://pluginbox.sourceforge.net/ 40
    • 學海無涯苦作舟 41
    • 相關電子書• Maven By Example – http://www.sonatype.com/index.php/Support/Books/Maven-By- Example• Maven: The Complete Reference – http://www.sonatype.com/index.php/Support/Books/Maven-The- Complete-Reference• Developing with Eclipse & Maven – http://www.sonatype.com/index.php/Support/Books/Developing- with-Eclipse-Maven 42
    • Maven 也不是萬能的• Maven 的限制 – http://community.jboss.org/wiki/MavenProblems• 為什麼 Hibernate 使用 Gradle 建置 – http://community.jboss.org/wiki/GradleWhy 43
    • 44