找回密码
 立即注册
首页 业界区 安全 Maven 构建知识库

Maven 构建知识库

訾颀秀 2025-8-8 17:35:43
Maven 构建知识库

1. 安装本地jar或者pom到构建仓库
  1. mvn install:install-file -Dfile=cs-1.1.3-SNAPSHOT.jar -DgroupId=com.bireturn -DartifactId=cs -Dversion=1.1.3-SNAPSHOT -Dpackaging=jar
  2. 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
  1. <dependency>
  2.     <version>1</version>
  3.     <groupId>com.herbert</groupId>
  4.     projectLocal.jar</artifactId>
  5.     <scope>system</scope>
  6.     <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/projectLocal.jar</systemPath>
  7. </dependency>
复制代码
3. Maven构建编译依赖本地jar
  1. <plugin>
  2.    
  3.     <groupId>org.apache.maven.plugins</groupId>
  4.     maven-compiler-plugin</artifactId>
  5.     <version>3.1</version>
  6.     <configuration>
  7.         <compilerArguments>
  8.             <extdirs>${basedir}/src/main/resources/lib/bo;${basedir}/src/main/resources/lib/cognos</extdirs>
  9.         </compilerArguments>
  10.     </configuration>
  11. </plugin>
复制代码
4. Maven构建运行外部程序
  1. <plugin>
  2.    
  3.     <groupId>org.codehaus.mojo</groupId>
  4.     exec-maven-plugin</artifactId>
  5.     <version>3.2.0</version>
  6.     <executions>
  7.         <execution>
  8.             <id>copy api to others project</id>
  9.             <phase>package</phase>
  10.             <goals>
  11.                 <goal>exec</goal>
  12.             </goals>
  13.             <configuration>
  14.                 <executable>cmd</executable>
  15.                
  16.                     /c echo done copy version .</argument>
  17.                     && copy /Y ${project.build.directory}\${project.artifactId}.jar D:\\lib\${project.artifactId}.jar </argument>
  18.                 </arguments>
  19.             </configuration>
  20.         </execution>
  21.     </executions>
  22. </plugin>
复制代码
5. Maven构建运行class程序
  1. <plugin>
  2.    
  3.     <groupId>org.codehaus.mojo</groupId>
  4.     exec-maven-plugin</artifactId>
  5.     <version>3.2.0</version>
  6.     <executions>
  7.         <execution>
  8.             <id>convert version code</id>
  9.             <phase>prepare-package</phase>
  10.             <goals>
  11.                 <goal>java</goal>
  12.             </goals>
  13.             <configuration>
  14.                 <includeProjectDependencies>true</includeProjectDependencies>
  15.                 <includePluginDependencies>true</includePluginDependencies>
  16.                 <mainClass>com.herbert.common.HkDoConvertVersionCode</mainClass>
  17.                
  18.                     微信小游戏地心侠士</argument>
  19.                 </arguments>
  20.             </configuration>
  21.         </execution>
  22.     </executions>
  23. </plugin>
复制代码
6. Maven构建自定义zip文件
  1. <plugin>
  2.    
  3.     <groupId>org.apache.maven.plugins</groupId>
  4.     maven-assembly-plugin</artifactId>
  5.     <version>3.3.0</version>
  6.     <executions>
  7.         <execution>
  8.             <id>rootZip-assembly</id>
  9.             <phase>package</phase>
  10.             <goals>
  11.                 <goal>single</goal>
  12.             </goals>
  13.             <configuration>
  14.                 <finalName>${name}</finalName>
  15.                 false</appendAssemblyId>
  16.                 <descriptors>
  17.                     <descriptor>./rootZip-assembly.xml</descriptor>
  18.                 </descriptors>
  19.             </configuration>
  20.         </execution>
  21.     </executions>
  22. </plugin>
复制代码
rootZip-assembly.xml 文件内容
  1.     <id>bin-1</id>
  2.     <formats>
  3.         <format>zip</format>
  4.     </formats>
  5.     <includeBaseDirectory>false</includeBaseDirectory>
  6.     <fileSets>
  7.         <fileSet>
  8.             <directory>src/main/webapp/WEB-INF</directory>
  9.             <outputDirectory>/herbert/WEB-INF</outputDirectory>
  10.             <excludes>
  11.                 <exclude>cfgHome/hkInitPath/initCmd/**</exclude>
  12.                 <exclude>cfgHome/hkInitPath/command/*.command</exclude>
  13.             </excludes>
  14.         </fileSet>
  15.         <fileSet>
  16.             <directory>target/classes</directory>
  17.             <outputDirectory>/herbert/WEB-INF/classes</outputDirectory>
  18.         </fileSet>
  19.         <fileSet>
  20.             <directory>src/main/webapp/apps_res</directory>
  21.             <outputDirectory>/herbert/apps_res</outputDirectory>
  22.         </fileSet>
  23.                <fileSet>
  24.                              <directory>src/scripts</directory>
  25.                              <outputDirectory>/</outputDirectory>
  26.                              <filtered>true</filtered>
  27.                    </fileSet>
  28.     </fileSets>
  29.     <dependencySets>
  30.                 <dependencySet>
  31.                         <outputDirectory>/lib</outputDirectory>
  32.                         <scope>runtime</scope>
  33.                         <excludes>
  34.                                 <exclude>${project.groupId}:${project.artifactId}</exclude>
  35.                         </excludes>
  36.                 </dependencySet>
  37.                 <dependencySet>
  38.                         <outputDirectory>/</outputDirectory>
  39.                         <includes>
  40.                                 <include>${project.groupId}:${project.artifactId}</include>
  41.                         </includes>
  42.                 </dependencySet>
  43.         </dependencySets>
  44. </assembly>  
复制代码
复制文件时可以对某些文件内容做替换,比如src/scripts 文件中的一个文件内容,打包后,对应参数会替换成具体的值
  1. java -jar -Dloader.path=resources,lib ${project.artifactId}-${project.version}.jar shutdown
复制代码
7. 筛选项目不同的class单独构建一个jar文件
  1. <plugin>
  2.       
  3.     <groupId>org.apache.maven.plugins</groupId>
  4.     maven-jar-plugin</artifactId>
  5.     <executions>
  6.         <execution>
  7.             <id>api</id>
  8.             <phase>package</phase>
  9.             <goals>
  10.                 <goal>jar</goal>
  11.             </goals>
  12.             <configuration>
  13.                 <classifier>herbert-dxxs-api</classifier>
  14.                
  15.                     <manifest>
  16.                         false</addClasspath>
  17.                     </manifest>
  18.                 </archive>
  19.                 <includes>
  20.                     <include>**/com/dxxs/DataImport*.class</include>
  21.                     <include>**/com/dxxs/DeleteFile*.class</include>
  22.                     <include>**/com/dxxs/DownFile*.class</include>
  23.                     <include>**/com/dxxs/ExcelConvert*.class</include>
  24.                     <include>**/com/dxxs/FileListener*.class</include>
  25.                     <include>**/com/dxxs/FileUpaload*.class</include>
  26.                     <include>**/com/dxxs/UploadFileInfo*.class</include>
  27.                 </includes>
  28.             </configuration>
  29.         </execution>
  30.     </executions>
  31. </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属性)
定义属性:
  1. <project>
  2.    <properties>
  3.        <mysql.version>5.6.32</mysql.version>
  4.    </properties>
  5. </project>
复制代码
使用属性:
  1. <dependency>
  2.     <groupId>mysql</groupId>
  3.      mysql-connector-java</artifactId>
  4.      <version>${mysql.version}</version>
  5. </dependency>
复制代码

  • setting.xml文件属性(与pom属性同理,用户可以用settings.开头的属性引用setting.xml文件的xml元素值)

    • ${settings.localRepository}表示本地仓库的地址

  • Java系统属性(所有的Java属性都可以使用Maven属性引用)

    • mvn help:system 可以查看所有的Java属性
      System.getProperties() 可以得到所有的Java属性
      ${user.home} 表示用户目录

  • 环境变量属性(所有的环境变量都可以以env.开头的Maven属性引用)

    • mvn help:system 可查看所有的环境变量
    • ${env.JAVA_HOME} 表示JAVA_HOME环境变量的值

9. Maven只构建父工程或只构建子工程

只构建父工程
mvn package -N
只构建子工程,多个子工程逗号分隔
mvn package -pl 子模块名称

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册