Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile dependency in maven?

Tags:

maven

Scenario:

I have a main level project A and within A , two child projects B and C worked on by different developers , but they agree on the abstraction through common interfaces.

B depends on C (dependency). In B's pom I added

<dependency> .. details of project C..</dependency> .

Doing this, maven inserts the dependencies fine except that project C is not recompiled.

I want project C to automatically re-compile every time I compile B.

like image 812
SQC Avatar asked Feb 06 '12 20:02

SQC


People also ask

What is compile dependency in Maven?

Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime.

How do I run a dependency 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.

Does maven compile download dependencies?

Most features in Maven – like compiling code, running tests and downloading dependencies – are implemented inside a plugin. To download dependencies, you can use the Maven Dependency Plugin.

What is the way to compile Maven application?

To build a Maven project via the command line, you use the mvn command from the command line. The command must be executed in the directory which contains the relevant pom file. You pass the build life cycle, phase or goal as parameter to this command.


3 Answers

If you want to build B and automatically build it's dependencies you can use the advanced options of the maven reactor like –-also-make-dependents .

mvn clean install –-projects B –-also-make 

Or short

mvn clean install -pl B -am

That will compile all submodules of A whose B depends on . There are a useful post on sonatype blog on the advanced options of maven reactor. http://www.sonatype.com/people/2009/10/maven-tips-and-tricks-advanced-reactor-options/

like image 86
mmounirou Avatar answered Oct 16 '22 13:10

mmounirou


List projects B and C as modules in the pom of the project A. Now when you build project A, it should build project B and C automatically and in the correct order.

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>multi</groupId>
    <artifactId>A</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <modules>
        <module>B</module>
        <module>C</module>
    </modules>
</project>
like image 27
Abhinav Sarkar Avatar answered Oct 16 '22 14:10

Abhinav Sarkar


I often use Maven reactor plugin to deal with these type of problems. This plugin even covers tough requirements that a complex project with many sub modules in a complex structure may has. See link for examples.

For above situations, using

mvn reactor:make -Dmake.folders=B 

to build B and C (and all dependencies of B if any).

Hope this helpful.

like image 41
bnguyen82 Avatar answered Oct 16 '22 14:10

bnguyen82