Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add local project (not jar) as a dependency to a Maven project

Tags:

I have two Maven projects that I've added to one Intellij Idea's project as two modules. Project B depends on Project A.

Here are simplified versions of their pom.xml files.

Project A:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.group</groupId>     <artifactId>a</artifactId>     <version>1.0-SNAPSHOT</version>     <packaging>jar</packaging>     <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <configuration>                     <source>1.8</source>                     <target>1.8</target>                 </configuration>             </plugin>         </plugins>     </build>      <dependencies>     </dependencies> </project> 

Project B:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.group</groupId>     <artifactId>b</artifactId>     <version>1.0-SNAPSHOT</version>     <packaging>jar</packaging>     <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <configuration>                     <source>1.8</source>                     <target>1.8</target>                 </configuration>             </plugin>         </plugins>     </build>      <dependencies>         <dependency>             <groupId>com.group</groupId>             <artifactId>a</artifactId>             <version>1.0-SNAPSHOT</version>         </dependency>     </dependencies> </project> 

Project A compiles easily since it has no dependency. But Project B depends on Project A and since I'm not telling maven how to find it, it can not be compiled with mvn package. But if I'm not mistaken, this was possible using Intellij Idea's "Meven Projects" menu since to Intellij Idea both projects are known.

But right now, for some unknown reason, I can not compile project B even in Intellij Idea. When I do, it prompts:

[ERROR] Failed to execute goal on project b: Could not resolve dependencies for project com.group:b:jar:1.0-SNAPSHOT: Could not find artifact com.group:a:jar:1.0-SNAPSHOT -> [Help 1] 

My question is, how can I include one project into other as its dependency? Please note that I'm not looking for a local dependency injection in maven since I want this to work in the future when I upload my packages into some repository.

like image 357
Mehran Avatar asked May 19 '17 15:05

Mehran


People also ask

How do I add a project as a dependency of another project in Maven?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.


1 Answers

Your project B depends on project A and you have already specified this as a dependency in your pom file. The only reason project B is not able to find project A may be due because of project A artifact is not installed in your local .m2 repository. So you have to first do a mvn clean install of your project A which will create the artifact in your maven repository. You should be then able to use artifact A in project B.

like image 190
Nitin Prabhu Avatar answered Oct 04 '22 00:10

Nitin Prabhu