惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
IT之家
IT之家
F
Full Disclosure
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
B
Blog
H
Help Net Security
The Cloudflare Blog
Recorded Future
Recorded Future
P
Proofpoint News Feed
P
Proofpoint News Feed
C
Cisco Blogs
T
Tailwind CSS Blog
P
Palo Alto Networks Blog
D
Docker
爱范儿
爱范儿
Know Your Adversary
Know Your Adversary
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Y
Y Combinator Blog
雷峰网
雷峰网
AWS News Blog
AWS News Blog
D
DataBreaches.Net
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
Blog — PlanetScale
Blog — PlanetScale
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
小众软件
小众软件
G
Google Developers Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
O
OpenAI News

博客园 - _herbert

小程序拍证件并局部裁剪 信创-为什么ORACLE使用JDBC查询SYSDATE时,RS.getDate能获取到时间部分? linux宝塔面板使用API自动部署更新文件 JSON JAVA找出哪个类import了不存在的类 LocalDate,LocalDateTime,Date,日期串相互转换 PostMan加载三方JS PostMan内置对象 微信支付集成_JSAPI VUE中使用AXIOS包装API代理 spring中el表达式安全和扩展 Spring使用el表达式 springboot中File默认路径 Springboot启动时记录进程ID ORACLE解析游标生成JSON ORACLE游标序列化 AI教我一条SQL实现明细转树形结构 Springboot构建包使用BOOT-INF中的class覆盖依赖包中的class MAVEN构建分离依赖JAR JAVA实现读取最后几行日志 信创-ORACLE迁移到KingbaseV9 信创-ORACLE迁移到DM8 sql分组 group by rollup,cube,grouping sets,group_id,groupingId
Maven 构建知识库
_herbert · 2025-08-08 · via 博客园 - _herbert

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>&amp;&amp; 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} 表示用户目录
  • 环境变量属性(所有的环境变量都可以以env.开头的Maven属性引用)

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

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

只构建父工程
mvn package -N

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