Praneel PIDIKITI
Praneel PIDIKITI

Reputation: 19537

Dependency for WebProject in maven

i want to create a web project(war)..which will depend on other jar(java project)

Once i build my war it should automatically build the java project to jar and add it to its local repository.

Can anyone help?

Upvotes: 1

Views: 472

Answers (4)

Siri
Siri

Reputation: 207

you can have multi module maven project.. hope that is what you are looking for.. like below

  <modules>
    <module>example-jar</module>
  </modules>

check this out: multimodule-web-spring-sect-simple-parent

Upvotes: 3

Nishant
Nishant

Reputation: 55886

Yes. Sure you can, and it is probably the most common use-case. Most of the web projects has a shared library that contains utility code shared among various other project.

There are two ways, to do this:

  1. Create a parent project, under that create your web project and other project at the same level. In your Web project add the Jar project as dependency. Now, whenever you will build the parent project using mvn clean install, you will have both of the projects built and installed in your local repository.

    parent
      +------- pom.xml (has two modules webProject and jarProject)
      +------- webProject
      |          + pom.xml (depends on jarProject)
      +------- jarProject
                 + pom.xml
    
  2. There is another way, using Maven reactor plug-in, this plug-in enables you to build all related projects to a project. You can see details here. Particularly, reactor:make-dependents

Upvotes: 2

Stephen C
Stephen C

Reputation: 719709

This is doesn't quite make sense. If the WAR depends on the JAR, then the JAR needs to be build and added to the local repository before the WAR is built (not after, as you have written in your question). (It has to be done in this order so that the JAR file can be included in the WAR file.)

Assuming that you really mean that the JAR should be built first, this is easy. Just create a parent-module, with child modules for the JAR and the WAR, and declare that the WAR module depends on the JAR module. Then build the parent module.

I recommend that you take the time to read the Multi-module Projects chapter of "Maven by Example", and or the relevant documentation on the Maven site.

Upvotes: 1

KevinS
KevinS

Reputation: 7882

If your jar project and war project follow the same release cycle, it would be a good idea to create a common parent pom and make the 2 projects modules of that parent.

Then you can build from the parent directory and Maven's reactor will always ensure that the jar project is built first.

If they don't follow the same release cycle, then you will probably just have to make sure that you manually build them in the right order.

If running these builds in Hudson, the maven plugin can ensure that a build of a downstream project is triggered after the completion of an upstream project.

Upvotes: 1

Related Questions