Have you read any of the documentation regarding Maven at all? If you have your projects defined using Maven then you don't have to do any special to get the dependencies to work.
For your needs you have two choices: Create two separate projects where one depends on the other OR you can create a multi-module project where one module still depends on the other.
Two separate projects
.
|-- A
| `-- pom.xml
| `-- src
| `-- main
| `-- java
`-- B
`-- pom.xml
`-- src
`-- main
`-- java
A/pom.xml
<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.company</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
B/pom.xml
<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.company</groupId>
<artifactId>B</artifactId>
<version>2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
That's it! You have a dependency on project/module A in B.
You build project A first by issuing mvn install
(and being in directory A). The artifact will be installed in your local repo (and available for other projects).
Then you build project B by issuing mvn install
(and being in directory B). The artifact will be installed in your local repo.
One parent project with sub-modules
.
|-- A
| `-- pom.xml
| `-- src
| `-- main
| `-- java
`-- B
| `-- pom.xml
| `-- src
| `-- main
| `-- java
`-- pom.xml
pom.xml
<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.company</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>A</module>
<module>B</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
The <depencdencyManagement/>
is there to help sub-modules pick the correct versions.
A/pom.xml
<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>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.company</groupId>
<artifactId>A</artifactId>
</project>
B/pom.xml
<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>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.company</groupId>
<artifactId>B</artifactId>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>A</artifactId>
</dependency>
</dependencies>
</project>
With this solution you only have to position yourself in the parent folder and run mvn install
(or mvn package
or the like). You will now build both A and B in the correct order and this whole project can be imported in Eclipse for example.