• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Building Tools

A Web Application Using Maven in Eclipse IDE

September 15, 2015 By j2eereference Leave a Comment

A Web Application Using Maven in Eclipse:

In this post we are going to see how to create a web application using Maven in Eclipse. There are many advantages of creating a Web application using Maven. For that we need to have Maven Plug-in integrated into Eclipse.

From Eclipse, New Project Wizard, select Maven Project as shown in the picture and click Next

Screenshot from 2015-09-13 12:37:56

In the next window, we will see options to skip the archetype selection. Leave it unchecked. Select the workspace location and click Next.

Screenshot from 2015-09-13 13:03:08

The next Window will show a list archetypes. We have to choose one archetype based what kind of project we want to build. Since we are going to build a web app, we’ll choose maven-archetype-webapp. Click Next.

Screenshot from 2015-09-13 13:23:32

In the next window, enter the GroupId, ArtifactId, Version and Package as required. Click Finish.

Screenshot from 2015-09-13 13:37:45

On successful build,   /src folder and pom.xml will be created in the file system. When we look at the pom.xml that is created,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<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.org.j2eereference</groupId>
  <artifactId>mywebapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mywebapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.4</source>
                <target>1.4</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

Note the packaging is ‘war’, because we have selected a web application selected in the archetype. Look at the dependencies. It has dependencies on servlet-api and jsp-api. These dependencies will be used if we are going to use servlet and jsps. We have junit dependency  which we will be used to run our test cases.

We have <build></build> section at the end which has build plug-ins. There is one plug-in defined that is ‘maven-compiler-plugin’. What to be noted here is that the compiler in Maven is actually a plug-in. What is happening in this plug-in section is, we are configuring how the compiler plug-in should behave. We have <configuration> tag with some values to define it. What happens if we do not use this mention this plug-in in the pom.xml? Maven uses this plug-in by default with default behaviour even if it is not mentioned in the pom.xml. When we run the mvn compile command, it is this maven compiler plug-in that is doing the job. The only reason we would need to mention this in pom.xml is to override the default behaviour of this plug-in and change the configuration.

Now all we need to do is export the project as WAR file, drop this .war into a container (tomcat probably) and it should work. Lets look at what will be there inside /src folder. We haven’t written any code yet. If we open /src/main/java/com/org/j2eereference (the package structure), there won’t be any java class by default. But inside /src/main/webapp, we will find WEB-INF folder and index.jsp. WEB-INF will contain the web.xml, which will be pointing the only available jsp that is index.jsp. Deploy this .war int tomcat container and run it.

Filed Under: Building Tools Tagged With: Maven, Web app

Adding dependency in Maven

August 10, 2015 By j2eereference Leave a Comment

To add a maven dependency, we have to make changes in pom.xml to include that dependency. Maven scans the pom.xml and downloads the required jar for the added dependency.

We have a sample project with one simple java class in this path : \MavenTestApp\src\main\java\com\org\j2eereference

App.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.org.j2eereference;
 
/**
* Hello world!
*
*/
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

This is our pom.xml in the \src folder.

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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>
 
  <groupId>com.org.j2eereference</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>MavenTestApp</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

In the pom.xml, notice that the only dependency we have is ‘junit’.

In App.java class, we will try to add ‘logging framework’ dependency. We will use the slf4j logging framework to log the test message. If we were mot using Maven, we would download the slf4j jars and add it to the classpath. Only after that we would be able to compile any class file that has dependency for those classes in the jar. With Maven we don’t have to do these steps. Maven automatically does all these steps for us.

App.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.org.j2eereference;
 
import org.slf4j.*;
 
/**
* Hello world!
*
*/
public class App
{
    public static void main( String[] args )
    {
        //System.out.println( "Hello World!" );
Logger logger = LoggerFactory.getLogger(App.class);
logger.info("Hello World!");
    }
}

Lets run mvn compile and see what happens

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
C:\MyApp\MavenTestApp> mvn compile
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTestApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenTestApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\MyApp\MavenTestApp\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/MyApp/MavenTestApp/src/main/java/com/org/j2eereference/App.java:[3,1] package org.slf4j does not exist
[ERROR] /C:/MyApp/MavenTestApp/src/main/java/com/org/j2eereference/App.java:[14,9] cannot find symbol
  symbol:   class Logger
  location: class com.org.j2eereference.App
[ERROR] /C:/MyApp/MavenTestApp/src/main/java/com/org/j2eereference/App.java:[14,25] cannot find symbol
  symbol:   variable LoggerFactory
  location: class com.org.j2eereference.App
[INFO] 3 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.826 s
[INFO] Finished at: 2015-08-06T22:08:00+05:30
[INFO] Final Memory: 10M/121M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project MavenTestApp: Compilation failure: Compilation failure:
[ERROR] /C:/MyApp/MavenTestApp/src/main/java/com/org/j2eereference/App.java:[3,1] package org.slf4j does not exist
[ERROR] /C:/MyApp/MavenTestApp/src/main/java/com/org/j2eereference/App.java:[14,9] cannot find symbol
[ERROR] symbol:   class Logger
[ERROR] location: class com.org.j2eereference.App
[ERROR] /C:/MyApp/MavenTestApp/src/main/java/com/org/j2eereference/App.java:[14,25] cannot find symbol
[ERROR] symbol:   variable LoggerFactory
[ERROR] location: class com.org.j2eereference.App
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

We get the above error. Note that the error message says it failed to execute the maven compiler plugin. It says that org.slf4j package does not exist because it cannot do an import as there is no such class in the classpath and hence it cannot resolve the symbols ‘Logger’ and ‘LoggerFactory’. This problem is because we don’t have the jar file in place.  So, we need to tell Maven to pull up the jar files from central repository. To do that, we need to add a dependency for slf4j in pom.xml with proper groupId, artifactId and version of slf4j. These are the coordinates for Maven to identify any jar file. How do we find this information?? A simple google search for ‘maven repository search’ would help us here.  It will list out Maven repositories. Pick any one and search for slf4j inside the repository. It will give all the information we need. It will also show the maven coordinates to be included in the pom.xml for this dependency. It is here : <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>

After adding this dependency in the pom.xml, run  mvn compile.

What happens now is, before MAven issues the compile command, it scans the pom.xml and finds that new dependency added and downloads slf4j jars from maven repository. Once all the jars are downloaded, Maven includes them in the classpath to makes it available for compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
C:\MyApp\MavenTestApp> mvn compile
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTestApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.pom
3/3 KB  
        
Downloaded: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.pom (3 KB at 1.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-parent/1.7.12/slf4j-parent-1.7.12.pom
4/12 KB  
8/12 KB  
12/12 KB  
          
Downloaded: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-parent/1.7.12/slf4j-parent-1.7.12.pom (12 KB at 18.7 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar
4/32 KB    
8/32 KB  
12/32 KB  
16/32 KB  
20/32 KB  
24/32 KB  
28/32 KB  
32/32 KB  
32/32 KB  
          
Downloaded: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar (32 KB at 27.4 KB/sec)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenTestApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\MyApp\MavenTestApp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.876 s
[INFO] Finished at: 2015-08-06T23:04:42+05:30
[INFO] Final Memory: 13M/159M
[INFO] ------------------------------------------------------------------------

In the log we can notice that, it connects to the Maven repository and downloads the slf4j jars. This time build has been successful. The /target/classes folder is generated that contains the .class files.

Filed Under: Building Tools Tagged With: Adding dependency, build tool, Maven, Maven compile, pom

Maven Build Phases

July 24, 2015 By j2eereference Leave a Comment

Maven Build Phases:

In any Application development process, once we are done with the coding, we need to build and deploy the code. We can use Maven for this purpose. Using maven we can build the application with just couple of commands (Ex : mvn compile, mvn package).These simple commands can take care of most of the things. If we have to compare it with Ant, we would have to specify everything that Ant has to do. Ex, if there were ten classes , we would instruct Ant to compile and package all the ten classes, of course using the wild cards to specify the class names.  But every step has to be explicitly mentioned. It is not the same with Maven. It has a standard directory structure and it knows what needs to be done. For example, Maven assumes code inside /src/main to be the actual source code and the code inside /src/test to be the test cases. Depending on this, it compiles and packages the code inside /src/main and does not package the /src/test but uses the /src/test to run the test cases before packaging.

Build Life Cycle :

  • Every build follows a life cycle – write the code, compile it, test it, package it and install it to an online repository so that  it can be used other projects. This is an ideal build life cycle.
  • It is split into different phases. Compile is a build phase, test is a build phase, etc. All these phases together comprise of build life cycle in Maven.
  • If we do not explicitly configure build life cycle, Maven chooses the default life cycle (will be detailed later). For example, as explained above, compile assumes /main to be the default source code directory and /test to be the test cases directory. This is the default behavior of the compile phase has in Maven. Similar default behavior exists  for other phases as well. Take for example 3 phases – compile, test and package. In order to do a package,  we would ideally first need to do  a compile and a test (test need not be necessary, if we want our code to be tested before packaging, we include test phase).
  • The other default behavior Maven has is, once you specify a phase, it automatically executes the previous phases. If we specify package, Maven automatically runs the compile phase and the test phase.

Some build phases :

  1. validate : checks if everything is in order – configuration, pom.xml, the code. This is usually not called explicitly. Due to the default behavior of automatically calling the previous phases, it gets executed as a previous phase.
  2. compile : compiles all the java files into .class files. When we run the compile phase directly, it automatically runs the ‘validate’ phase.
  3. test : the phase where we run  our test cases specified for the code. Say for example we have MyApp.java as our application class and MyAppTest.java as our test casefor MyApp java class, ‘test’ automatically knows that there is a test case for the code we are trying to compile and runs those test cases after the ‘compile’ phase. So again, if we directly run the ‘test’ phase, Maven automatically runs ‘validate’ and ‘compile’ before the ‘test’.
  4. package : packages all the .class files. Again executes all the other phases and packages. Suppose, there is a bug and the ‘test’ phase fails, there is no point in packaging the code. So ‘package’ executes only after ‘validate’, ‘compile’ and ‘test’ are successfully executed. It ends up in a .jar or .war or. ear file depending on what is specified in pom.xml.
  5. install : installs the packaged artifact into the local Maven repository. Note that it doesn’t install into the application server. install here is maven-specific.
  6. deploy : the final phase often confused due to the name it has. Like ‘install’, it doesn’t deploy the artifact into the application server, we run this phase when we want to publish our artifact to a remote repository where other users can download and consume it for their applications. This is not something we really when we develop applications unless the code we write can be shared on a repository.

In order to run any of these phases, we need to run the command – mvn <phaseName> in command prompt from the directory where pom.xml is present.

Ex : To do a compile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
C:\MyApp\MavenTestApp>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTestApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenTestApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\MyApp\MavenTestApp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.182 s
[INFO] Finished at: 2015-07-23T23:13:46+05:30
[INFO] Final Memory: 10M/121M
[INFO] ------------------------------------------------------------------------
 
C:\MyApp\MavenTestApp>

‘compile’ checks for the projects, it finds that there is a pom.xml in the same directory. Then finds the .java files in the /src directory and compiles. There is only one source file in the example, that is compiled and saved as .class file in /target/classes.

To run test :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
C:\MyApp\MavenTestApp>mvn test
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTestApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenTestApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenTestApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MyApp\MavenTestApp\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenTestApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenTestApp ---
[INFO] Surefire report directory: C:\MyApp\MavenTestApp\target\surefire-reports
 
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.org.j2eereference.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.312 s
[INFO] Finished at: 2015-07-23T23:28:45+05:30
[INFO] Final Memory: 10M/219M
[INFO] ------------------------------------------------------------------------
 
C:\MyApp\MavenTestApp>

Maven pulls up all the test case available in the /test directory and runs them. From the logs we can find that, before executing the ‘test’, it tries to compile the code.

1
2
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenTestApp ---
[INFO] Nothing to compile - all classes are up to date

Since, the code was already compiled, it says there is nothing to compile and everything is up to date.

Similarly, we can package the code using the command- mvn package . This command again, tries to compile and test. On successful compilation and tests, it packages the artifact and places it in /target folder.

To install the artifact in the local repository – mvn install .  This command runs from ‘validate’ phase all over again and installs the artifact to local repository on the machine. When we run this command, we can notice the logs and see something similar to below statements that confirms the artifact install into the local repository.

1
[INFO] Installing C:\MyApp\MavenTestApp\pom.xml to C:\Users\Admin\.m2\repository\com\org\j2eereference\MavenTestApp\1.0-SNAPSHOT\MavenTestApp-1.0-SNAPSHOT.pom

We can notice that the default location of the local repository, i.e., /m2/repository.

Filed Under: Building Tools

Maven Archetypes and pom.xml

July 22, 2015 By j2eereference Leave a Comment

Maven Archetypes

The advantage of using Maven is that, it does a lot of things behind the scenes. We don’t have to configure each and every step – creating the source code directories, configuring the dependencies, compiling and packaging etc.

Project Template:

Once we download and configure maven in development environment, we start off with configuring the project structure using maven.
From the Project folder in command prompt, we run below command to set up the project.
mvn archetype:generate
When we run this command, it creates the folder structure and pom.xml. This is downloaded from the maven repository depending the inputs we provide. The inputs that we provide for ‘mvn archetype:generate’ are:

1. Archetype : This is the information to Maven as to what is the kind of project we need. Maven has a huge list of archetypes. Each archetype corresponds to a particular type of application that we want to develop. It decides the layout of the files, the folder structure and the dependencies that are required. Each archetype is a good starting point for the type of application we want to build.

2. Group ID : It is an explanatory ID that we give to group all the  artifacts. It is analogous to package structure. For example : com.org. j2eereference

3. Artifact ID : Identifies the output that project generates. If it is web app, it would be a war file. If it is an enterprise app, it would be an ear file. It is analogous to the  name of the jar/war/ear that we want to create.

4. Version : The version of the application. When we are starting the application, we normally keep the default value of 1. With different releases of the application, this version number keeps increment.

5. Package : The name of the package where we need to place our source code. It is an information to maven as to what package a particular class belongs to. Depending the input we provide here, a particular java class will be assigned to the package.

Build:

All the information that we provide as input to archetype (group id, artifact id, version, package) will be available in pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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>
 
  <groupId>com.org.j2eereference</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>MavenTestApp</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

We have a sample pom.xml that was generated when we ran ‘mvn archetype:generate’.

  • Notice that we have groupId, artifactId and version in the pom.xml which are the keys for any particular artifact. If we have an artifact and want to publish it to a repository, Maven identifies the artifact using the combination of these 3 keys.
  • We also have a packaging tag which tells Maven what is the actual output – a jar or war or an ear. In the example, it is a jar and this again depends on archetype. If we had chosen archetype for a web application, this would probably be a war.
  • name tag specifies the name of the application.
  • properties tag tells that the source code encoding is UTF-8
  • Now have a look at the dependencies tag. <dependencies> will have the list of all the dependencies, each specified inside a <dependency> tag. In the example, there is only one dependency, i.e., junit. To specify the dependency, we again use the combination of groupId, artifactId and version. Say for example, junit releases a new version. The version number would change to, say 3.8.2. The groupId and artifact would remain same and to refer to the newer version, we only need to update the version number. Suppose we want to use a different artifactId for the same groupId, we would change the artifactId to whatever artifactId and update the version as accordingly.

So, the structure is like a tree, where groupId is the highest level which has lots of artifacts inside it. We can choose the right artifact by mentioning the artifactId and the version of the artifact.

Scope is something that tells when to use the particular artifact (will be detailed in upcoming posts). This is used by Maven when building and packaging the application.

This is how maven uses gruopId, artifactId and version to pull up an artifact from the repository.

Now the below code should make sense.

1
2
3
<groupId>com.org.j2eereference</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>

Our groupId, artifactId and version number should be in such a way that other applications can consume our artifact if we choose to release this and save it in a repository. This is applicable for other reasons as well. Suppose we do not choose to publish in an online repository but going to use it for internal consumption. For that, Maven has an internal repository on the local machine itself. Whenever we build and package any artifact, maven installs the artifact into the local repository.  Suppose we have a different web application that uses this jar (MavenTestApp-1.0-SNAPSHOT.jar in the example), all we need to do is, in the pom.xml of that application, we need to add the dependency for this jar.

1
2
3
4
5
<dependency>
<groupId>com.org.j2eereference</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

What Maven does is, it will first look in the local repository for this jar and check if such an artifact is available. If it available, Maven will pull it up from local repository. If it is not available, it will go to the online repository.

This is the main information available in pom.xml

1. maven co-ordinates : artifactId, groupId, version etc

2. meta data : name and version of the project

3. Build information : says the project is jar, war or an ear

4. Resources and dependencies : the resources and dependencies required to successfully build the project

Filed Under: Building Tools Tagged With: archetype, Maven, pom

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.