[英]How to bundle/package latest version of custom library in maven web project without updating pom.xml?
我有一个自定义库-Dao.jar,其中包含数据库持久性逻辑。
每当代码如下所示时,我都会将此罐子推送到新版本的工件中:
mvn install:install-file -Dfile=C:\*****\target\Dao.jar -DgroupId=non-public.com.karthik -DartifactId=dao -Dversion=2.0 -Dpackaging=jar
我有另一个Maven Web项目,它依赖于此jar。 这个jar也打包/捆绑在maven webapp项目/ war中。
<dependency>
<groupId>non-public.com.karthik</groupId>
<artifactId>dao</artifactId>
<version>2.0</version>
</dependency>
当前,每当工件中有新版本的Dao.jar可用时,我都会在pom.xml中更改dao依赖项的版本,并重新构建maven webapp项目。
是否可以在不手动更改pom.xml中的依赖版本的情况下,使用最新版本的Dao.jar来构建Maven项目?
当Maven搜索依赖项时,它首先检查本地存储库( 〜/ .m2 / repository )。 如果找不到,它将尝试其他资源,例如POM文件或设置文件( 〜/ .m2 / settings.xml )中定义的远程存储库。
按照这种逻辑,如果您尝试使用尚未安装到本地资源库中的本地项目版本,则Maven将永远无法找到要在另一个项目中使用的版本。
为避免始终更改版本号并手动构建两个项目。 您可以为两个项目都创建一个父POM。 然后,父级将能够识别出其中一个子项目依赖于另一个,并以正确的顺序构建它们。
根据Luciano的输入,我创建了一个具有2个模块(Dao和Web)的多模块Maven项目/父POM。
父级
<groupId>com.karthik</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
..........
</dependencies>
</dependencyManagement>
<modules>
<module>web</module>
<module>dao</module>
</modules>
子模块#1-dao
<parent>
<groupId>com.karthik</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dao</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc6</artifactId>
</dependency>
.........
</dependencies>
子模块2-Web(在POM中声明为dao依赖性)
<parent>
<groupId>com.karthik</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.karthik</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
.........
</dependencies>
当我在父pom的根路径上运行mvn package命令时,将同时构建两个模块-web.war和dao.jar。 此方法确保始终将最新版本的dao.jar打包在web.war中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.