Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jar dependend on classes in war project

Tags:

java

maven-2

Suppose I have a war and jar projects defined in maven.

The Jar project is dependent on the War project

I have managed to get this working in package mode by adding

In the war project <attachClasses> to true in the war plugin.

And making a dependency to

        <groupId>com</groupId>
        <artifactId>oneway-delegator</artifactId>
        <version>1.0</version>
        <classifier>classses</classifier>

in the jar pom.

But when running only compile the classes jar is not created and everything fails.

Any ideas folks ??

like image 997
Roman Avatar asked Mar 24 '10 11:03

Roman


People also ask

Can we add WAR as dependency in Maven?

It worked great with classes originating from external libraries which I could add as dependencies. However, it failed with classes originating from the Hippo CMS Site module because it was compiled into a . war archive which Maven can't handle as a dependency.

What is dependency jar?

a dependency management utility for jar files. It's primary purpose is to traverse through a directory, parse each of the jar files in that directory, and identify the dependencies between the jar files. The output is an xml file representing the PhysicalDependencies between the jar files.

What is type in Maven dependency?

There are two types of dependencies in Maven: direct and transitive. Direct dependencies are the ones that we explicitly include in the project.


2 Answers

But when running only compile the classes jar is not created and everything fails.

You can indeed configure the maven-war-plugin to package/deploy the classes and resources included in your webapp as an "attached" JAR artifact with a classifier with the following configuration:

<project>
  ...
  <artifactId>mywebapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>X.Y</version>
        <configuration>
          <attachClasses>true</attachClasses>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

But this artifact is created during the package phase so don't expect it to be there if you run compile only.

Note that this configuration option has been introduced for a very particular use case, the skinny war use case. If you need to re-use this JAR in another project the recommended approach is in general to move the classes to a separate module that builds a JAR, and then declare a dependency on that JAR from your webapp as well as from any other projects that need it.

like image 60
Pascal Thivent Avatar answered Oct 13 '22 01:10

Pascal Thivent


Don't do this.

That is not an acceptable dependency to make a jar (plain java code) dependent on a war (Java EE specific application package). If you have code in your war that you are dependent on, then that code should be in it's own jar and then both the web app and the jar project would have a common dependency on it.

like image 35
Robin Avatar answered Oct 13 '22 00:10

Robin