📜 ⬆️ ⬇️

Build JavaEE application by maven

When I translated my ejb3 projects to maven, I had a lot of trouble with setting up the project, connecting the “correct” libraries. Information or not, or it was outdated. I use JBoss 5.1.0.GA in my work, so all the examples will be based on it.
Task. You need to create a maven2 project for JBoss. Let it be an Enterprise Application. Inside there is an ejb3 module and a web application.

The biggest difficulty with the maven + JBoss bundle is the selection of dependencies. JBoss is a fairly dynamic project. Over time, its structure changes, the necessary classes crawl from one artifact to another. To determine the necessary dependencies, I developed a whole algorithm:

As a rule, this is enough to find the necessary dependencies.

The directory structure, according to the ideology of maven, is set in advance. The entire project is described in a special pom.xml file. With the help of individual settings in this file you can redo the whole structure. But it’s better not to do this - pom.xml will make it harder for others to understand what and where your project is going to be difficult.

The project will be integral. The root module, which sets the general settings, and "child" modules. Each module is a separate application (ejb3, war, ear). Create a directory structure
# ejb-3
mkdir -p jee-project/ejb/src/main/java
mkdir -p jee-project/ejb/src/main/resources
mkdir -p jee-project/ejb/src/test/java
#web
mkdir -p jee-project/web/src/main/java
mkdir -p jee-project/web/src/main/resources
mkdir -p jee-project/web/src/main/webapp
mkdir -p jee-project/web/src/test/java
#ear
mkdir -p jee-project/ear

')
Now create the pom.xml for the root module. File quote fully to describe some of its elements.


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

     <! - Group identifier.  Just a way to group projects.  ->
     <groupId> my.labs </ groupId>

     <! - ID of the project itself.  ->
     <artifactId> jee-project </ artifactId>

     <! - An important point.  Indicates the "type" of the project.  If the project contains subprojects
        (modules) - "pom" must be specified ->
     <packaging> pom </ packaging>

     <! - Version.  SNAPSHOT indicates that work is underway.  Ideology maven suggests
        that development is on SHAPSHOT-versions.  By readiness with the help of special
        release procedures.  It is better to follow the generally accepted pattern, less
        surprises will be ->
     <version> 1.0-SNAPSHOT </ version>

     <! - We list the modules included in the current project, in fact - this is the name of the directories,
        in which maven will look for child modules.  ->
     <modules>
         <module> ejb </ module>
         <module> web </ module>
         <module> ear </ module>
     </ modules>

     <! - In the dependencyManagement block, the dependencies are specified, which are found throughout the project,
        including  in modules.  In the subprojects themselves, it suffices to refer to the dependencies already mentioned.  ->
     <dependencyManagement>
         <dependencies>

             <! - junit will be used for testing, as discussed by <scope> test </ scope> ->
             <dependency>
                 <groupId> junit </ groupId>
                 <artifactId> junit </ artifactId>
                 <version> 4.4 </ version>
                 <scope> test </ scope>
             </ dependency>

             <! - dependency set for JavaEE ->
             <dependency>
                 <groupId> javax.annotation </ groupId>
                 <artifactId> jsr250-api </ artifactId>
                 <version> 1.0 </ version>
                 <scope> provided </ scope>
             </ dependency>
             <dependency>
                 <groupId> javax.ejb </ groupId>
                 <artifactId> ejb-api </ artifactId>
                 <version> 3.0 </ version>
                 <scope> provided </ scope>
             </ dependency>
             <dependency>
                 <groupId> javax.jms </ groupId>
                 <artifactId> jms </ artifactId>
                 <version> 1.1 </ version>
                 <scope> provided </ scope>
             </ dependency>
             <dependency>
                 <groupId> javax.persistence </ groupId>
                 <artifactId> persistence-api </ artifactId>
                 <version> 1.0 </ version>
                 <scope> provided </ scope>
             </ dependency>
             <dependency>
                 <groupId> javax.servlet </ groupId>
                 <artifactId> servlet-api </ artifactId>
                 <version> 2.5 </ version>
                 <scope> provided </ scope>
             </ dependency>
         </ dependencies>
     </ dependencyManagement>

     <! - we specify the source code encoding and java version ->
     <build>
         <plugins>
             <plugin>
                 <groupId> org.apache.maven.plugins </ groupId>
                 <artifactId> maven-compiler-plugin </ artifactId>
                 <configuration>
                     <source> 1.6 </ source>
                     <target> 1.6 </ target>
                     <encoding> UTF-8 </ encoding>
                 </ configuration>
             </ plugin>
         </ plugins>
     </ build>
 </ project>




Everything, the head project is described. Now briefly about the subsidiary projects.
You need to put ejb in the maul
  <packaging> ejb </ packaging> 
and describe the ejb3 build settings
     <build>
         <plugins>
             <plugin>
                 <groupId> org.apache.maven.plugins </ groupId>
                 <artifactId> maven-ejb-plugin </ artifactId>
                 <version> 2.1 </ version>
                 <configuration>
                     <ejbVersion> 3.0 </ ejbVersion>
                 </ configuration>
             </ plugin>
         </ plugins>
     </ build>


Setting the ear module also has some of its features. All modules that we want to include in the final ear-archive should be specified in dependencies. When building maven in a chain, it will process the dependencies of the modules and include all jares with scope = compile into the archive. By default, these archives in application.xml will not be mentioned. To correct the situation, a special element is used.
     <build>
         <plugins>
             <plugin>
                 <groupId> org.apache.maven.plugins </ groupId>
                 <artifactId> maven-ear-plugin </ artifactId>
                 <version> 2.3.2 </ version>
                 <configuration>
                     <version> 5 </ version>

                     <! - we list modules ->
                     <modules>
                         <ejbModule>
                             <groupId> $ {project.groupId} </ groupId>
                             <artifactId> ejb </ artifactId>
                         </ ejbModule>
                         <webModule>
                             <groupId> $ {project.groupId} </ groupId>
                             <artifactId> web </ artifactId>
                         </ webModule>
                     </ modules>
                     <! - the same element that specifies the need to specify all libraries in application.xml ->
                     <includeLibInApplicationXml> true </ includeLibInApplicationXml>
                 </ configuration>
             </ plugin>
         </ plugins>
         <finalName> $ {project.parent.artifactId} </ finalName>
     </ build>


The build is a team
mvn package

Naturally, maven should be installed. When it is first launched, it will create the $ HOME / .m2 directory and will long and tediously download project dependencies and dependency dependencies. But this is the first time. All downloaded it will add to the local $ HOME / .m2 / repository cache and in the future will access it.
The topic information is most likely suitable for any J2EE server. But I'm not sure, it was tested on JBoss. If there are any corrections, I’m happy to include it here.

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


All Articles