如何在Maven项目中引入自己的jar包

僤鯓⒐⒋嵵緔 提交于 2019-12-22 00:59:18

 一.Maven项目打包的两种方式

二.如何优雅地修改多模块maven项目中的版本号 

三.一个项目使用另一个项目

 一.Maven项目打包的两种方式:

1.依赖工具比如eclipse

2.使用命令行:

使用cmd进入到pom对应的目录下:

执行:mvn  clean install 指令。

二.如何优雅地修改多模块maven项目中的版本号?

当我们用maven建立一个项目时,包含了多个子model,我们想把一个子model打包deploy到私服上去,需要:

1.从父节点把整个project都deploy上去,这时别人才能拉去到你上传的model。

2.保证整个project中所有model的version是一致的。

对于version,我们可以使用-SNAPSHOT这种方式,这样所有model都是一致的,每次发布也不会有问题。但如果项目发展比较快,需要使用release版本发布,由于release版本每次deploy时版本号不能重复,所以就需要每次都修改父model的version和子model的parent中的version。这时,就会有以下问题需思考:

正式版不能重复发布,所以版本号每次上线都要更改
当项目中包含几个子模块时,通常我们想让子模块的版本号跟父项目的版本号一致
子模块也会相互依赖
最容易解决的是问题3,maven有一个内置属性${project.version}表示的是项目的版本号,当一个子模块依赖其他子模块时我们可以这样写:

<parent>
        <groupId>parent-groupId</groupId>
        <artifactId>parent-artifactId</artifactId>
        <version>1.0.0</version>
        <relativePath>..</relativePath>
    </parent>
    <artifactId>module-artifactId</artifactId>
    <dependency>
        <artifactId>other-module-artifactId</artifactId>
        <groupId>other-module-groupId</groupId>
        <version>${project.version}</version>
    </dependency>

子POM的groupId和version可以省略,这样只要保证子模块的版本号都一致就不会有问题了。但是<parent>标签中的版本号还是要写,不然会报错,显然maven没有进化到这么智能的程度,或者可能会造成其他混乱而必须指定。而投机取巧地把<parent>标签中的版本号换成${project.version}也没有成功,因为必须先初始化<parent>标签才能知道${project.version}是多少。

但是maven除了内置属性之外还可以自定义属性,我们可以在父pom中这样写:

<groupId>parent-groupId</groupId>
    <artifactId>parent-artifactId</artifactId>
    <version>${parent-version}</version>
 
    <properties>
        <parent-version>1.0.0</parent-version>
    </properties>

在子pom中这样写:

<parent>
    <groupId>parent-groupId</groupId>
    <artifactId>parent-artifactId</artifactId>
    <version>${parent-version}</version>
    <relativePath>..</relativePath>
</parent>

这样写达到了只修改一处的目的,但是在mvn install时会提示 <parent> 标签中的version不是一个常量,未来的版本可能不支持,而且各个子模块单独mvn install时可能会失败。

最终比较折中的解决方案是使用了maven的插件来解决,在父pom中这样写:

前边废话了一大堆,后边的才是重点

<groupId>parent-groupId</groupId>
    <artifactId>parent-artifactId</artifactId>
    <version>1.0.0</version>
    <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <generateBackupPoms>false</generateBackupPoms>
            </configuration>
        </plugin>
    </plugins>
    </build>

 

只需要执行mvn -N versions:update-child-modules则会自动把子POM的<parent>标签中的version更新为和父POM一致。这样修改一处然后运行一下执行一下命令就可以达到统一修改版本号的目的了。(在父model上执行后,所有子model中parent中的version都会修改)

mvn  versions:update-child-modules: 自动把子POM的<parent>标签中的version更新为和父POM一致

mvn versions:set -DnewVersion=0.0.2-SNAPSHOT:更新的父及子Module的版本号都改成了0.0.2-SNAPSHOT.

mvn versions:commit :如果没有在父pom用引入插件,Maven还会生成一个pom.xml.versionsBackup的备份文件,还需要mvn versions:commit提交

如果没有在父pom用引入插件,

 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <generateBackupPoms>false</generateBackupPoms>
            </configuration>
        </plugin>

Maven还会生成一个pom.xml.versionsBackup的备份文件,还需要mvn versions:commit提交

 或者执行

mvn versions:set -DnewVersion=2.0.0-SNAPSHOT -f pom.xml

然后项目中的依赖以及聚集的依赖版本都会被一起更新,也可以采用通配符来进行匹配

mvn versions:set -DgroupId=org.apache.maven.* -DartifactId=* -DoldVersion=2.* -DnewVersion=2.1.0-SNAPSHOT

 三.一个项目使用另一个项目

1.打成jar包引入项目依赖,详见:

如何在Maven项目中引入自己的jar包

2.maven工程项目与项目之间的依赖方式

参考:https://blog.csdn.net/qq_28524127/article/details/80475398

 

首先看一下项目结构:

 

 

1、需要在父工程中把子工程为坐标引进来,同时标注父工程为pom工程:

 

 

2、同时在父工程中把子工程当作一个模块引进来

 

 子工程的version可以写成

<version>${project.version}</version>

 

3、需要在每一个子项目中通过parent标签,标注为父工程,代表他需要依赖的父工程。同时拥有父工程的所有依赖:


问题:那这种形式的项目,怎么部署在Tomcat上啊?父工程也要部署在Tomcat上吗?还是只需要子工程部署在Tomcat上就可以了?

 子工程部署在Tomcat上就可以了,会以jar的形式加载父工程

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!