Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a maven project build its own dependencies?

Tags:

java

maven-2

With maven is it possible to have a top-level project who's packaging type is "war" which will build itself and all of its dependent modules (packaged as jar) and have the build generate a project.war file?

Much of the documentation examples and other examples I've seen often use a top-level project with packaging type of "pom" and the project only serves the purpose of tying the modules together. Can I avoid this?

So basically I need something which is effectively like declaring a <module>my-module</module> for maven to build, and in that same POM, declaring a <dependency>...my-module's artifact...</dependency> on that same module which needs to be built. Maybe a plugin as someone already suggested?

Update: In other words (to simplify the problem): If I have project A and project B, where project A depends on project B - is there a way for me to execute a build on project A and also have it automatically build project B (and include project B as its dependency - creating projectA.war which contains projectB.jar)?

like image 205
oym Avatar asked Dec 01 '10 23:12

oym


People also ask

How can Maven project be used as dependency?

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.

What are the build dependencies used in Maven?

There are two types of dependencies in Maven: direct and transitive. Direct dependencies are the ones that we explicitly include in the project. On the other hand, transitive dependencies are required by direct dependencies. Maven automatically includes required transitive dependencies in our project.

Are Maven dependencies compiled?

Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath.

What happens when we build a Maven project?

Maven makes the day-to-day work of Java developers easier and helps with the building and running of any Java-based project. Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install and Deploy.


2 Answers

super_aardvark suggested correct way but,
For requirement I would suggest following structure It is suitable and good structure also :

Consedering ProjectA as project-webapp , ProjectB as project-core

You can have following structure :

Your Grand Project :

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

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany.project</groupId>
    <artifactId>project</artifactId>
    <version>2.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Project Repository System</name>
    <description>Project Repository System R2</description>

    <modules>
        <module>project-core</module>
        <module>project-webapp</module>
    </modules>
 </project>

Your WebApp Project:

<?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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.mycompany.project</groupId>
        <artifactId>project</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>project-webapp</artifactId>
    <version>2.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Project Web Application</name>
    <description>Project Repository</description>
    <dependency>
            <groupId>com.mycompany.project</groupId>
            <artifactId>project-core</artifactId>
            <version>2.0-SNAPSHOT</version>
     </dependency>

 </project>

Your Core Project:

<project>
    <parent>
        <groupId>com.mycompany.project</groupId>
        <artifactId>project</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>project-core</artifactId>
    <version>2.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Project Core</name>
    <description>ProjectCore</description>

 </project>

Your Directory structure should look like:

-------Grand Parent.pom
  |
  |--------project-webapp
  |                     |
  |                     project-webapp.pom
  | 
  | -------project-core.pom
                        |
                       project-core.pom

From parent pom execute mvn clean install it will build both the web-app and core project

like image 141
jmj Avatar answered Nov 12 '22 15:11

jmj


That's not really what a top-level project is for. Your WAR project has dependencies, which are the artifacts (e.g. jars) that will be included in the WAR (in WEB-INF/lib) when you run 'mvn package'. Your WAR project pom can have the top-level project as its parent, but it shouldn't be the parent of its dependencies. You may want to have that top-level project be the parent of both the WAR project and of the JAR projects that are dependencies in the WAR.

like image 23
super_aardvark Avatar answered Nov 12 '22 16:11

super_aardvark