0% found this document useful (0 votes)
26 views

Building Java Projects With Maven

Uploaded by

nitishjha8828
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Building Java Projects With Maven

Uploaded by

nitishjha8828
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Building Java Projects with Maven

Create the directory structure

In a project directory of your choosing, create the following subdirectory


structure;

└── src
└── main
└── java
└── hello

Within the src/main/java/hello directory, you can create any Java classes
you want. To maintain consistency with the rest of this guide, create these
two classes: HelloWorld.java and Greeter.java .

src/main/java/hello/HelloWorld.java

package hello;

public class HelloWorld {


public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}

src/main/java/hello/Greeter.java

package hello;

public class Greeter {


public String sayHello() {
return "Hello world!";
}
}
Download XML Notepad

Define a simple Maven build

Maven projects are defined with an XML file named pom.xml. Among other
things, this file gives the project’s name, version, and dependencies that it
has on external libraries.

Create a file named pom.xml at the root of the project (i.e. put it next to
the src folder) and give it the following contents:

pom.xml

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

<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer

implementation="org.apache.maven.plugins.shade.resource.ManifestRes
ourceTransformer">

<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Build Java code

mvn compile
mvn package

To execute the JAR file run:

java -jar target/gs-maven-0.1.0.jar


mvn install

Declare Dependencies

First, change HelloWorld.java to look like this:

src/main/java/hello/HelloWorld.java

package hello;
import org.joda.time.LocalTime;

public class HelloWorld {


public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
System.out.println("The current local time is: " +
currentTime);
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}

Now run mvn compile command again. Now build will fail because you’ve not declared
Joda Time as a compile dependency in the build. You can fix that by adding
the following lines to pom.xml (within the <project> element):

<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>

Now if you run mvn compile or mvn package , Maven should resolve the Joda
Time dependency from the Maven Central repository and the build will be
successful.

Write a Test

First add JUnit as a dependency to your pom.xml, in the test scope:

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

Then create a test case like this, create new file and folders as given below:

src/test/java/hello/GreeterTest.java

package hello;

import static org.hamcrest.CoreMatchers.containsString;


import static org.junit.Assert.*;

import org.junit.Test;

public class GreeterTest {

private Greeter greeter = new Greeter();

@Test
public void greeterSaysHello() {
assertThat(greeter.sayHello(), containsString("Hello"));
}

Now execute again this command

mvn test

next time while running again, give this command to delete target folder created.

mvn clean test

You might also like