开发者

Having a maven project build its own dependencies?

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开发者_JAVA技巧 - creating projectA.war which contains projectB.jar)?


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


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.


This is not possible in Maven 1, 2 or 3.

I'd recommend to give up this idea, because Maven's whole purpose is to enforce standardized development process. Don't fight the structure, just create a parent POM module and make the WAR module and other dependencies underneath it.


When you have a multi-module project and you're doing work in several modules simultaneously it can be tedious and error-prone to make sure all the necessary dependencies are updated.

In my situation, I would like my build system to detect changes and only build the modules that are necessary. One way this might be possible with maven is for someone to write a custom plugin that does this, which doesn't seem insurmountable given there are already complex plugins available, like the maven release plugin.

Others have already mentioned the aggregation pom concept, which is repeatable and does produce the necessary artifacts. But sometimes you end up building more than you really need to.

Maven profiles can help and here's a good article in that regard:

Using Aggregate and Parent POMs

Also note in the article the concept of the batch pom, which I was not previously aware of.

Remember, mvn clean install will push your artifact into your local repo. So if module A depends on module B, as long as your local repo has the latest build of module B then you should be all set. So, if there were an external tool that was watching for changes to module B and automatically built it when there were and pushed those changes into the local repo then when module A was rebuilt it would pick up those changes. There are continuous integration (CI) tools that can do this, like Jenkins. But you would need a local install to have this work directly with your local repo. It's still an option, though.

Another option would be for the CI environment to push your builds to an external maven repo (or even one you setup locally with something like Nexus). Then you setup your CI builds to pull from that location as well.

So, there are solutions that rely on other tools or potential plugins to do what you want - just depends how much time and effort you want to invest to get it all setup. But, once you get over that hurdle you'll have a system (and knowledge and experience) that you can use on all your projects, not to mention you'll be familiar with how many development shops/teams work.

I would recommend researching continuous integration and continuous delivery for more information and ideas.


In parent pom, you have to define a sequential order of modules to be compiled. You can add a war packing module to the last in that list. It will simply aggregate all previous compiled code together.


Not really - (Well, I can think of a couple of ways, but I'd not use them as they're convoluted and go against the basic ethos/practices of Maven).

Don't forget that the other purpose of the top-level pom is to provide a single point to set common details such the particular versions of dependencies used in the modules of the project.


NetBeans has an option that allows you to do exactly this with Maven projects but I don't know any pure Maven solutions. I think that the task is more suited for an IDE, because it knows for what depended projects you have the code (based of what projects you have opened in the workspace). How would Maven itself differentiate between a dependency that you want to build and one that needs to be fetched from the repository. And for those that need to be built, where should it look for the source code?

Anyway, another solution to the problem, that I used successfully a few times, is to create a simple shell script that navigates to your projects folders and starts the build then it waits for it to finish then proceeds to the next project and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜