中國廣東省深圳市望海路半島城邦三期
518067
+86 13113668890
<netkiller@msn.com>
html
MMDVM Hotspot:
YSF80337 - CN China 1 - W24166/TG46001
BM_China_46001 - DMR Radio ID 4600441java
$Date: 2013-04-10 15:03:49 +0800 (Wed, 10 Apr 2013) $git
2009-12-12github
版權 © 2009-2019 Neo Chanweb
版權聲明apache
轉載請與做者聯繫,轉載時請務必標明文章原始出處和做者信息及本聲明。微信
|
|
|
|
系列文檔:maven
Netkiller Linux 手札 | Netkiller FreeBSD 手札 | Netkiller Shell 手札 |
Netkiller Security 手札 | Netkiller Web 手札 | Netkiller Monitoring 手札 |
Netkiller Storage 手札 | Netkiller Mail 手札 | Netkiller Virtualization 手札 |
對於大型互聯網項目,不可能把全部代碼都放在一個項目目錄下,經常會將一個項目拆分紅多個子項目單元測試
例如一個電商系統測試
在開發過程當中經常會遇到這種需求,有一個部分代碼是共用的,全部項目都會使用到。因此須要將這部分代碼獨立成一個項目。
測試目錄深度
Project | |--- common -> https://example.com/xxxx/common.git | | ---pom.xml |--- project1 | |--- pom.xml |--- project2 | |--- pom.xml |---pom.xml
common 是公共項目,獨立倉庫。經過git submodule 技術掛載到項目目錄。project1,project2 構建依賴 common 項目產生的 jar 包。
例 2.2. Maven parent
<?xml version="1.0" encoding="UTF-8"?> <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>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>maven</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit.jupiter.version>5.4.0</junit.jupiter.version> </properties> <dependencies> </dependencies> <modules> <module>project1</module> <module>project2</module> <module>common</module> </modules> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> </plugins> </build> </project>
注意
<packaging>pom</packaging> 必須是 pom
項目下面的子模塊
<modules> <module>project1</module> <module>project2</module> <module>common</module> </modules>
例 2.3. watir-webdriver example
<?xml version="1.0"?> <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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>demo</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>common</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> </project>
添加 parent 標籤, 聲明項目的父子關係
<parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<?xml version="1.0"?> <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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>demo</groupId> <artifactId>project1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>project1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>demo</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
聲明項目的父子關係
<parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
因爲 project1 依賴 common 加入下面依賴
<dependency> <groupId>demo</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
project2 跟 project1 相似
在父項目目錄運行 mvn package
neo@MacBook-Pro ~/workspace/maven % mvn package [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] maven [pom] [INFO] common [jar] [INFO] project1 [jar] [INFO] project2 [jar] [INFO] [INFO] -----------------------------< demo:maven >----------------------------- [INFO] Building maven 0.0.1-SNAPSHOT [1/4] [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] ----------------------------< demo:common >----------------------------- [INFO] Building common 0.0.1-SNAPSHOT [2/4] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ common --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/common/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ common --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ common --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/common/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ common --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ common --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] [INFO] Results: [INFO] [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ common --- [INFO] Building jar: /Users/neo/workspace/maven/common/target/common-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/demo/common/pom.xml already added, skipping [INFO] META-INF/maven/demo/common/pom.properties already added, skipping [INFO] [INFO] ---------------------------< demo:project1 >---------------------------- [INFO] Building project1 0.0.1-SNAPSHOT [3/4] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ project1 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project1/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ project1 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project1 --- [INFO] Building jar: /Users/neo/workspace/maven/project1/target/project1-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/demo/project1/pom.xml already added, skipping [INFO] META-INF/maven/demo/project1/pom.properties already added, skipping [INFO] [INFO] ---------------------------< demo:project2 >---------------------------- [INFO] Building project2 0.0.1-SNAPSHOT [4/4] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project2 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project2/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ project2 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project2 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project2/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project2 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project2/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ project2 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project2 --- [INFO] Building jar: /Users/neo/workspace/maven/project2/target/project2-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/demo/project2/pom.xml already added, skipping [INFO] META-INF/maven/demo/project2/pom.properties already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for maven 0.0.1-SNAPSHOT: [INFO] [INFO] maven .............................................. SUCCESS [ 0.006 s] [INFO] common ............................................. SUCCESS [ 2.317 s] [INFO] project1 ........................................... SUCCESS [ 0.539 s] [INFO] project2 ........................................... SUCCESS [ 0.101 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.115 s [INFO] Finished at: 2019-02-28T17:03:29+08:00 [INFO] ------------------------------------------------------------------------ neo@MacBook-Pro ~/workspace/maven %
咱們能夠看到有三個包產生
neo@MacBook-Pro ~/workspace/maven % find . -iname '*.jar' ./project1/target/project1-0.0.1-SNAPSHOT.jar ./common/target/common-0.0.1-SNAPSHOT.jar ./project2/target/project2-0.0.1-SNAPSHOT.jar
咱們也能夠單獨編譯子項目
neo@MacBook-Pro ~/workspace/maven/project1 % mvn package [INFO] Scanning for projects... [INFO] [INFO] ---------------------------< demo:project1 >---------------------------- [INFO] Building project1 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ project1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project1/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project1/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ project1 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project1 --- [INFO] Building jar: /Users/neo/workspace/maven/project1/target/project1-0.0.1-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.015 s [INFO] Finished at: 2019-02-28T17:09:18+08:00 [INFO] ------------------------------------------------------------------------
共享庫 common-0.0.1-SNAPSHOT.jar 已經安裝到 ~/.m2 目錄下。
neo@MacBook-Pro ~/workspace/maven/project1 % find ~/.m2 -iname 'common-0.0.1-SNAPSHOT.jar' /Users/neo/.m2/repository/demo/common/0.0.1-SNAPSHOT/common-0.0.1-SNAPSHOT.jar
Maven 子模塊建立方法 https://www.netkiller.cn/java/build/maven.html#maven.module
目錄結構
Project | |--- common (Shared) | | ---pom.xml |--- project1 (depend common) | |--- pom.xml |--- project2 (depend common) | |--- pom.xml |---pom.xml
構建 父項目
pipeline { agent { label "default" } stages { stage("檢出") { steps { checkout( [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], userRemoteConfigs: [[url: env.GIT_REPO_URL]]] ) } } stage("構建") { steps { echo "構建中..." sh 'mvn package -Dmaven.test.skip=true' // mvn 示例 archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物 echo "構建完成." } } stage("測試") { steps { echo "單元測試中..." // 請在這裏放置您項目代碼的單元測試調用過程,例如: sh 'mvn test' // mvn 示例 echo "單元測試完成." junit '**/target/surefire-reports/*.xml' // 收集單元測試報告的調用過程 } } stage("部署") { steps { echo "部署中..." echo "部署完成" } } } }
構建共享項目
pipeline { agent { label "default" } stages { stage("檢出") { steps { checkout( [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], userRemoteConfigs: [[url: env.GIT_REPO_URL]]] ) } } stage("構建") { steps { echo "構建中..." dir(path: 'common') { sh 'mvn package -Dmaven.test.skip=true' // mvn 示例 archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物 } echo "構建完成." } } stage("測試") { steps { echo "單元測試中..." sh 'mvn test' // mvn 示例 echo "單元測試完成." junit 'target/surefire-reports/*.xml' // 收集單元測試報告的調用過程 } } stage("部署") { steps { echo "部署中..." dir(path: 'common') { sh 'mvn install' } echo "部署完成" } } } }
構建 project1 和 project2
pipeline { agent { label "default" } stages { stage("檢出") { steps { checkout( [$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], userRemoteConfigs: [[url: env.GIT_REPO_URL]]] ) } } stage("共享庫") { steps { echo "構建中..." dir(path: 'common') { sh 'mvn install -Dmaven.test.skip=true' // mvn 示例 archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物 } echo "構建完成." } } stage("構建") { steps { echo "構建中..." dir(path: 'project1') { sh 'mvn package -Dmaven.test.skip=true' // mvn 示例 archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // 收集構建產物 } echo "構建完成." } } stage("測試") { steps { echo "單元測試中..." sh 'mvn test' // mvn 示例 echo "單元測試完成." junit 'target/surefire-reports/*.xml' // 收集單元測試報告的調用過程 } } stage("部署") { steps { echo "部署中..." // 部署腳本 echo "部署完成" } } } }