Apache Maven是軟件(主要針對java)項目管理及自動構建工具,由Apache軟件基金會所提供。Maven是主要用於Java項目的構建。maven在猶太語中「蓄電池知識」, Maven描述瞭如何構建軟件並處理了依賴:基於項目對象模型(POM)。
Maven也可被用於構建和管理各類項目,例如C#,Ruby,Scala和其餘語言編寫的項目。Maven曾是Jakarta項目的子項目,現爲由Apache軟件基金會主持的獨立Apache項目。html
源碼下載:https://github.com/bava/gswm-book
做者郵件: Balaji@inflinx.com or Sudha@inflinx.com.
Maven的優勢java
標準的目錄結構python
依賴管理react
插件linux
統一的構建抽象git
工具支持:IDE和持續集成系統等。github
Archetype(相似模板)web
開源(Sonatype公司提供商業支持)spring
Maven的設計理念:CoC(Convention over configuration或coding by convention)
Maven相似產品:
Ant + Ivyt (http://ant.apache.org) 使用配置文件 build.xml。apache
<project name="Sample Build File" default="compile" basedir="."> <target name="compile" description="Compile Source Code"> <echo message="Starting Code Compilation"/> <javac srcdir="src" destdir="dist"/> <echo message="Completed Code Compilation"/> </target> </project>
Ant很靈活,由於容易搞得複雜。Apache Ivy (http://ant.apache.org/ivy/)爲Ant提供了加強的自動依賴管理。ivy.xml實例:
<ivy-module version="2.0"> <info organisation="com.apress" module="gswm-ivy" /> <dependencies> <dependency org="org.apache.logging.log4j" name="log4j-api" rev="2.0.2" /> </dependencies> </ivy-module>
Gradle (http://gradle.org/),使用Groovy-based Domain Specific Language (DSL)。綜合了Ant的靈活性和maven的約定及依賴管理。爲目前最火爆的java編譯工具,但容易致使複雜的構建。 build.gradle實例:
apply plugin: 'java' version = '1.0' repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.10' }
當前版本爲Maven 3.3.3, 要求JDK 1.7或以上版本。下載地址:http://maven.apache.org/download.cgi。
Linux下面下載以後解壓到/var/lib/apache-maven-3.3.3,並把/var/lib/apache-maven-3.3.3/bin加入PATH便可。
檢驗安裝:
# mvn -v Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00) Maven home: /var/lib/apache-maven-3.3.3 Java version: 1.7.0_79, vendor: Oracle Corporation Java home: /usr/java/jdk1.7.0_79/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "2.6.32-573.el6.x86_64", arch: "amd64", family: "unix"
幫助:
# mvn -h usage: mvn [options] [<goal(s)>] [<phase(s)>] Options: -am,--also-make If project list is specified, also build projects required by the list -amd,--also-make-dependents If project list is specified, also build projects that depend on projects on the list -B,--batch-mode Run in non-interactive (batch) mode -b,--builder <arg> The id of the build strategy to use. -C,--strict-checksums Fail the build if checksums don't match -c,--lax-checksums Warn if checksums don't match -cpu,--check-plugin-updates Ineffective, only kept for backward compatibility -D,--define <arg> Define a system property -e,--errors Produce execution error messages -emp,--encrypt-master-password <arg> Encrypt master security password -ep,--encrypt-password <arg> Encrypt server password -f,--file <arg> Force the use of an alternate POM file (or directory with pom.xml). -fae,--fail-at-end Only fail the build afterwards; allow all non-impacted builds to continue -ff,--fail-fast Stop at first failure in reactorized builds -fn,--fail-never NEVER fail the build, regardless of project result -gs,--global-settings <arg> Alternate path for the global settings file -gt,--global-toolchains <arg> Alternate path for the global toolchains file -h,--help Display help information -l,--log-file <arg> Log file to where all build output will go. -llr,--legacy-local-repository Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true -N,--non-recursive Do not recurse into sub-projects -npr,--no-plugin-registry Ineffective, only kept for backward compatibility -npu,--no-plugin-updates Ineffective, only kept for backward compatibility -nsu,--no-snapshot-updates Suppress SNAPSHOT updates -o,--offline Work offline -P,--activate-profiles <arg> Comma-delimited list of profiles to activate -pl,--projects <arg> Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path. -q,--quiet Quiet output - only show errors -rf,--resume-from <arg> Resume reactor from specified project -s,--settings <arg> Alternate path for the user settings file -T,--threads <arg> Thread count, for instance 2.0C where C is core multiplied -t,--toolchains <arg> Alternate path for the user toolchains file -U,--update-snapshots Forces a check for missing releases and updated snapshots on remote repositories -up,--update-plugins Ineffective, only kept for backward compatibility -V,--show-version Display version information WITHOUT stopping build -v,--version Display version information -X,--debug Produce execution debug output
maven的配置文件通常位於用戶目錄的 .m2目錄。全局設置存儲在/var/lib/apache-maven-3.3.3/conf/。最重要的配置文件是settings.xml。
使用mvn -X能夠看到相關的加載過程。默認的Settings.xml內容以下:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings>
關於配置文件的詳細介紹參見https://maven.apache.org/settings.html及https://maven.apache.org/guides/mini/guide-configuring-maven.html。
用pom.xml描述,Maven Central地址: repo.maven.apache.org 和 uk.maven.org. 默認的架構以下:
考慮到公司內部共享、安全、知識產權(不發佈本公司的包)、受權、帶寬和下載速度等,通常架構以下:
開源倉庫管理器有:
Sonatype Nexus www.sonatype.com/nexus
Apache Archiva http://archiva.apache.org/
Artifactory www.jfrog.com/open-source/
settings.xml中添加倉庫示例:
<?xml version="1.0" encoding="UTF-8" ?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/ settings-1.0.0.xsd"> ....... <profiles> <profile> <id>your_company</id> <repositories> <repository> <id>spring_repo</id> <url>http://repo.spring.io/release/</url> </repository> <repository> <id>jboss_repo</id> <url>https://repository.jboss.org/</url> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>your_company</activeProfile> </activeProfiles> ....... </settings>
倉庫添加在 pom.xml 中可移植性好,settings.xml通常用來存放企業級別的設置。
依賴一般是JAR, WAR, enterprise archive (EAR)和ZIP. 字段以下:
groupId: 如 org.hibernate.log4j和org.springframework.boot。
artifactId: 如 log4j。
version: 版本,如1.0.0, 2.3.1-SNAPSHOT和4.3.6.Final。
type: 如JAR, WAR和EAR.
Maven支持傳遞依賴,碰到版本衝突時,深度優先,若是同一深度,則先碰到的優先。注意儘可能不要有版本衝突。
依賴的範圍有:
compile(默認, build, test和run可見)
provided: build、test可見,好比 Servlet api, JSP api。
runtime: run可見
test: test可見
system: 相似 provided,可是不從倉庫獲取,寫死到文件系統。
import: 適用.pom文件,Maven 2.0.9及之後版本支持。
還能夠手工加載,好比:
# mvn install:install-file -DgroupId=com.apress.gswmbook -DartifactId=test -Dversion=1.0.0 -Dfile=/root/gswm-book/chapter3/test.jar -Dpackaging=jar -DgeneratePom=true [WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] Unrecognised tag: 'server' (position: START_TAG seen ...<!-- 115.236.185.202\u6d4b\u8bd5\u73af\u5883 -->\n\t<server>... @142:10) @ /var/lib/apache-maven-3.3.3/conf/settings.xml, line 142, column 10 [WARNING] [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom --- [INFO] Installing /root/gswm-book/chapter3/test.jar to /Repository/com/apress/gswmbook/test/1.0.0/test-1.0.0.jar [INFO] Installing /tmp/mvninstall7757910186748006943.pom to /Repository/com/apress/gswmbook/test/1.0.0/test-1.0.0.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.285 s [INFO] Finished at: 2015-11-18T17:20:37+08:00 [INFO] Final Memory: 6M/118M [INFO] ------------------------------------------------------------------------ [root@localhost chapter3]#
經常使用的目錄有src/main/resources、src/main/scripts、src/test/resources、src/main/webapp、src/it、src/main/db、src/site。
新建HelloWorld類:
public class HelloWorld { public void sayHello() { System.out.print("Hello World"); } }
新建HelloWorldTest類:
import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.io.IOException; /** Updated the test so that it is more platform independent */ public class HelloWorldTest { private final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); @Before public void setUp() { System.setOut(new PrintStream(outStream)); } @Test public void testSayHello2() throws IOException { HelloWorld hw = new HelloWorld(); hw.sayHello(); outStream.flush(); String printedOutPut = new String(outStream.toByteArray()); String[] outputData = printedOutPut.split(System.getProperty("line.separator")); Assert.assertEquals("Hello World", outputData[0]); } @After public void cleanUp(){ System.setOut(null); } }
pom.xml配置以下:
<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.apress.gswmbook</groupId> <artifactId>gswm</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Getting Started with Maven</name> <url>http://apress.com</url> <properties> <junit.version>4.12</junit.version> </properties> <developers> <developer> <id>balaji</id> <name>Balaji Varanasi</name> <email>balaji@inflinx.com</email> <properties> <active>true</active> </properties> </developer> <developer> <id>sudha</id> <name>Sudha Belida</name> <email>sudha@inflinx.com</email> <properties> <active>true</active> </properties> </developer> </developers> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> <exclusions> <exclusion> <groupId> org.hamcrest</groupId> <artifactId>hamcrest</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
注意上面有junit支持,並禁止org.hamcrest的傳遞依賴,執行'mvn dependency:tree", 再「mvn package「,便可編譯成功。
執行mvn compiler:compile
上面的 compiler 就是插件,compile是目標。
相似的有mvn clean:clean,mvn plugin_identifier:goal_identifier
插件能夠在pom.xml中配置,好比強行用java6編譯:
<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> <!-- Project details omitted for brevity --> <dependencies> <!-- Dependency details omitted for brevity --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
<build /> 有個很是有用的子tag: finalName.默認爲<<project_artifiact_id>>-<<project_version>> 。
maven的生命週期以下:
Default: 編譯、打包、部署
Clean: 清理
Site: 生成文檔和site
階段:
Validate: 運行檢查確保該項目是正確的,全部依賴下載並可用。
Compile:編譯源代碼。
Test:使用框架運行單元測試。這一步不要求應用程序打包。
Package:打包代碼爲發佈格式,如JAR或WAR。
Install:安裝包到本地倉庫。
Deploy: 推安裝包到遠程倉庫。
<packaging>jar</packaging>會自動指定目標。
一般測試失敗,打包也會失敗,能夠經過mvn package –Dmaven.test.skip=true 不執行測試階段。
python測試開發精華羣 291184506 PythonJava單元白盒測試 144081101