War project dependency in maven

I need to access one bean class from a war project into my other military project. The bean class exists in MyProject. I wrote a pom of another project called NewProject as follows.

<groupId>MyProject</groupId> <artifactId>MyProject</artifactId> <version>1</version> </parent> <artifactId>MyProject</artifactId> <packaging>war</packaging> 

Can military addiction be added to another military project?

+6
source share
5 answers

If you configure maven-war-plugin with the following attribute:

 <attachClasses>true</attachClasses> 

You will receive an additional artifact with the following coordinates:

 <dependency> <groupId>myGroup</groupId> <artifactId>myArtifact</artifactId> <version>myVersion</myVersion> <classifier>classes</classifier> </dependency> 

which contains all the classes within your military project, which can be used as a dependency, which is a jar file that will help solve your problem.

+16
source

In your military project

 <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <attachClasses>true</attachClasses> </configuration></plugin> 

Creates an artifact of classes that you can use in your desired project

 <dependency> <groupId>your-group-id</groupId> <artifactId>your-artifact-id</artifactId> <version>your-version</version> <classifier>classes</classifier> 

send maven war plugin

hope this helps ...

+4
source

You better create an IMOO jar with your war classes that are needed in your project.

And then just add the dependency to your project configuration ( classifier classes).

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>classes</id> <phase>compile</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>classes</classifier> </configuration> </execution> </executions> </plugin> 
+2
source

Dependencies work using jars, and you usually define a common dependency in a .jar file that both .war s can access. This is not Maven-specific, but how dependencies in Java work. The internal structure of a .war differs from ajar in how classes are laid out.

Therefore, in Maven, I would expect to define a .jar project, and then two .war projects as depending on the original project.

+1
source

Move reusable classes to a separate module. This will help:

  • Test things faster.

  • Use code as a dependency in other projects.

Alternatively, you can only create jar classes using the maven-jar-plugin and creating an artifact based on the classifier. However, I think my suggestion is better in that it gives you a clear separation of the code and makes you better organize the code.

+1
source

Source: https://habr.com/ru/post/955960/


All Articles