简体   繁体   中英

How to bundle/package latest version of custom library in maven web project without updating pom.xml?

I have a custom library - Dao.jar which contains the database persistence logic.
I push this jar to artifactory with new version each time there is a change in code as shown below :

mvn install:install-file -Dfile=C:\*****\target\Dao.jar -DgroupId=non-public.com.karthik -DartifactId=dao -Dversion=2.0 -Dpackaging=jar

I have another maven web project which has a dependency on this jar. This jar is also packaged/bundled in the maven webapp project/war.

<dependency>
    <groupId>non-public.com.karthik</groupId>
    <artifactId>dao</artifactId>
    <version>2.0</version>
</dependency>

Currently, I am changing the version of dao dependency in the pom.xml & re-building the maven webapp project each time a new version of Dao.jar is available in the artifactory.
Is there any option to build the maven project with the latest version of Dao.jar without manually changing the dependency version in the pom.xml?

When Maven searches for a dependency, it first checks the local repository ( ~/.m2/repository ). If it's not found, it tries other resources, such as remote repositories defined in the POM file or in the settings file ( ~/.m2/settings.xml ).

By that logic, if you try to use a version of a local project that's not yet installed to the local repository, Maven will never be able to find it to use in another project.

To avoid changing version numbers all the time and manually building both projects. You could create a parent POM for both projects. The parent would then be able to recognize that one of the child projects depends on the other and build them in the correct order.

Based on Luciano's inputs, I have created a multi-module maven project/parent POM with 2 modules(dao & web)

Parent

<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>

Child module # 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>

Child module # 2 - web(declared dao dependency in POM)

 <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>

When I run mvn package command at root path of parent pom, both modules - web.war and dao.jar are built. This method ensures always the latest version of dao.jar is packaged in web.war.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM