Maven 构建知识库

Maven 构建知识库

1. 安装本地jar或者pom到构建仓库

bash 复制代码
 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

xml 复制代码
<!--小游戏 地心侠士 公众号:小满小慢 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

xml 复制代码
<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构建运行外部程序

xml 复制代码
<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>&amp;&amp; copy /Y ${project.build.directory}\${project.artifactId}.jar D:\\lib\${project.artifactId}.jar </argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

5. Maven构建运行class程序

xml 复制代码
<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文件

xml 复制代码
<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 文件内容

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 文件中的一个文件内容,打包后,对应参数会替换成具体的值

bat 复制代码
java -jar -Dloader.path=resources,lib ${project.artifactId}-${project.version}.jar shutdown

7. 筛选项目不同的class单独构建一个jar文件

xml 复制代码
<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}表示项目版本
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> p r o j e c t . b a s e d i r 同 {project.basedir}同 </math>project.basedir同{basedir}
    • ${project.baseUri}表示项目文件地址
    • ${maven.build.timestamp}表示项目构建开始时间
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> m a v e n . b u i l d . t i m e s t a m p . f o r m a t 表示 {maven.build.timestamp.format}表示 </math>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
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> p r o j e c t . v e r s i o n 表示项目的 v e r s i o n ,同 {project.version}表示项目的version,同 </math>project.version表示项目的version,同{version}
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> p r o j e c t . b u i l d . f i n a l N a m e 表示项目打包输出文件的名称 , 默认为 {project.build.finalName}表示项目打包输出文件的名称,默认为 </math>project.build.finalName表示项目打包输出文件的名称,默认为{project.artifactId}${project.version}
  • 自定义属性(在pom.xml文件的标签下定义的Maven属性)

定义属性:

xml 复制代码
<project>
   <properties>
       <mysql.version>5.6.32</mysql.version>
   </properties>
</project>

使用属性:

xml 复制代码
<!--小游戏 地心侠士 公众号:小满小慢 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} 表示用户目录
  • 环境变量属性(所有的环境变量都可以以env.开头的Maven属性引用)

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

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

只构建父工程 mvn package -N

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

相关推荐
白应穷奇8 分钟前
编写高性能数据处理代码 01
后端·python
设计师小聂!15 分钟前
力扣热题100-------169.多数元素
java·数据结构·算法·leetcode·多数元素
杨充19 分钟前
03.接口vs抽象类比较
前端·后端
一只叫煤球的猫21 分钟前
基于Redisson的高性能延迟队列架构设计与实现
java·redis·后端
卡尓22 分钟前
使用 Layui 替换 Yii 基础模板的默认 Bootstrap 样式并尝试重写导航栏组件
后端
WhyWhatHow26 分钟前
JEnv:新一代Java环境管理器,让多版本Java管理变得简单高效
java·后端
保加利亚的风38 分钟前
【Java】使用FreeMarker来实现Word自定义导出
java·word
SteveCode.42 分钟前
SpringBoot 2.x 升 3.x 避坑指南:企业级项目的实战问题与解决方案
java·spring boot
Rust语言中文社区1 小时前
Rust 训练营二期来袭: Rust + AI 智能硬件
开发语言·后端·rust
Yang-Never1 小时前
Kotlin -> object声明和object表达式
android·java·开发语言·kotlin·android studio