Maven 構建知識庫
Maven 構建知識庫
1. 安裝本地jar或者pom到構建倉庫
mvn install:install-file -Dfile=cs-1.1.3-SNAPSHOT.jar -DgroupId=com.bireturn -DartifactId=cs -Dversion=1.1.3-SNAPSHOT -Dpackaging=jar
mvn install:install-file -Dfile=cs-1.1.3-SNAPSHOT.pom -DgroupId=com.bireturn -DartifactId=cs -Dversion=1.1.3-SNAPSHOT -Dpackaging=pom
2. Maven依賴當前項目相對jar
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<dependency>
<version>1</version>
<groupId>com.herbert</groupId>
<artifactId>projectLocal.jar</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/projectLocal.jar</systemPath>
</dependency>
3. Maven構建編譯依賴本地jar
<plugin>
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArguments>
<extdirs>${basedir}/src/main/resources/lib/bo;${basedir}/src/main/resources/lib/cognos</extdirs>
</compilerArguments>
</configuration>
</plugin>
4. Maven構建運行外部程序
<plugin>
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy api to others project</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd</executable>
<arguments>
<argument>/c echo done copy version .</argument>
<argument>&& copy /Y ${project.build.directory}\${project.artifactId}.jar D:\\lib\${project.artifactId}.jar </argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
5. Maven構建運行class程序
<plugin>
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>convert version code</id>
<phase>prepare-package</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>com.herbert.common.HkDoConvertVersionCode</mainClass>
<arguments>
<argument>微信小游戲地心俠士</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
6. Maven構建自定義zip文件
<plugin>
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>rootZip-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${name}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>./rootZip-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
rootZip-assembly.xml 文件內容
<assembly>
<id>bin-1</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/webapp/WEB-INF</directory>
<outputDirectory>/herbert/WEB-INF</outputDirectory>
<excludes>
<exclude>cfgHome/hkInitPath/initCmd/**</exclude>
<exclude>cfgHome/hkInitPath/command/*.command</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/herbert/WEB-INF/classes</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/webapp/apps_res</directory>
<outputDirectory>/herbert/apps_res</outputDirectory>
</fileSet>
<fileSet>
<directory>src/scripts</directory>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<scope>runtime</scope>
<excludes>
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
復制文件時可以對某些文件內容做替換,比如src/scripts 文件中的一個文件內容,打包后,對應參數會替換成具體的值
java -jar -Dloader.path=resources,lib ${project.artifactId}-${project.version}.jar shutdown
7. 篩選項目不同的class單獨構建一個jar文件
<plugin>
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>api</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>herbert-dxxs-api</classifier>
<archive>
<manifest>
<addClasspath>false</addClasspath>
</manifest>
</archive>
<includes>
<include>**/com/dxxs/DataImport*.class</include>
<include>**/com/dxxs/DeleteFile*.class</include>
<include>**/com/dxxs/DownFile*.class</include>
<include>**/com/dxxs/ExcelConvert*.class</include>
<include>**/com/dxxs/FileListener*.class</include>
<include>**/com/dxxs/FileUpaload*.class</include>
<include>**/com/dxxs/UploadFileInfo*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
8. Maven構建的一些全局變量
-
內置屬性(Maven預定義屬性,用戶可以直接使用)
- ${basedir}表示項目的根路徑,即包含pom.xml文件的目錄
- ${version}表示項目版本
- ${project.basedir}同$
- ${project.baseUri}表示項目文件地址
- ${maven.build.timestamp}表示項目構建開始時間
- ${maven.build.timestamp.format}表示${maven.build.timestamp}的展示格式,默認值為yyyyMMdd-HHmm
-
pom屬性(使用pom屬性可以引用到pom.xml文件中對應元素的值)
- ${project.build.sourceDirectory}表示主源碼路徑,默認為src/main/java/
- ${project.build.testSourceDirectory}表示測試源碼路徑,默認為src/test/java/
- ${project.build.directory}表示項目構建輸出目錄,默認為target/
- ${project.outputDirectory}表示項目測試代碼編譯輸出目錄,默認為target/classes/
- ${project.groupId}表示項目的groupId
- ${project.artifactId}表示項目的artifactId
- ${project.version}表示項目的version,同$
- ${project.build.finalName}表示項目打包輸出文件的名稱,默認為${project.artifactId}$
-
自定義屬性(在pom.xml文件的
標簽下定義的Maven屬性)
定義屬性:
<project>
<properties>
<mysql.version>5.6.32</mysql.version>
</properties>
</project>
使用屬性:
<!--小游戲 地心俠士 公眾號:小滿小慢 QQ:464884492-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
-
setting.xml文件屬性(與pom屬性同理,用戶可以用settings.開頭的屬性引用setting.xml文件的xml元素值)
- ${settings.localRepository}表示本地倉庫的地址
-
Java系統屬性(所有的Java屬性都可以使用Maven屬性引用)
- mvn help:system 可以查看所有的Java屬性
System.getProperties() 可以得到所有的Java屬性
${user.home} 表示用戶目錄
- mvn help:system 可以查看所有的Java屬性
-
環境變量屬性(所有的環境變量都可以以env.開頭的Maven屬性引用)
- mvn help:system 可查看所有的環境變量
- ${env.JAVA_HOME} 表示JAVA_HOME環境變量的值
9. Maven只構建父工程或只構建子工程
只構建父工程
mvn package -N
只構建子工程,多個子工程逗號分隔
mvn package -pl 子模塊名稱
- 轉載請注明來源
- 作者:楊瀚博
- QQ:464884492

浙公網安備 33010602011771號