Subject: The sub-level project -- app module
Author: Linux
In response to: The sub-level project -- util module
Posted on: 10/31/2017 05:25:18 AM
The source code:
$ nano /opt/maven/hello-one/app/src/main/java/com/xyz/integration/Hello.java
package com.xyz.integration;
import com.xyz.commons.*;
public class Hello {
String name = "World"; // default
public Hello(){
}
public Hello(String name){
this.name = name;
}
public String getName() {
return name;
}
public String getGreetings() {
return "Hello " + getName() + "!";
}
public static void main(String[] args) {
Hello hello = new Hello("World");
String msg = hello.getGreetings() + " Today is " + DateUtil.getToday();
System.out.println(msg);
}
}
The test code:
$ nano /opt/maven/hello-one/app/src/test/java/com/xyz/integration/HelloTest.java
package com.xyz.integration;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloTest {
@Test
public void testGetName() {
String actual = new Hello("World").getName();
String expected = "World";
assertEquals(expected, actual);
}
@Test
public void testGetGreetings() {
String actual = new Hello().getGreetings();
String expected = new Hello("World").getGreetings();
assertEquals(expected, actual);
}
}
pom.xml
$ nano /opt/maven/hello-one/app/pom.xml
<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>
<!-- parent coordinates -->
<parent>
<groupId>com.xyz</groupId>
<artifactId>hello-one-pom</artifactId>
<version>1.0</version>
</parent>
<!-- project coordinates -->
<groupId>com.xyz</groupId>
<artifactId>app-jar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<!-- project dependencies -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xyz</groupId>
<artifactId>util-jar</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
>
> On 10/31/2017 05:22:54 AM
Linux wrote:
The source code:
$ nano /opt/maven/hello-one/util/src/main/java/com/xyz/commons/DateUtil.java
package com.xyz.commons;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils; // the third party class
public class DateUtil {
public static String getToday() {
String today = DateFormatUtils.format(new Date(), "dd-MMM-yyyy");
return today;
}
}
The test code:
$ nano /opt/maven/hello-one/util/src/test/java/com/xyz/commons/DateUtilTest.java
package com.xyz.commons;
import static org.junit.Assert.assertEquals;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class DateUtilTest {
@Test
public void testGetToday() {
String actual = DateUtil.getToday();
String expected = new SimpleDateFormat("dd-MMM-yyyy")
.format(new Date());
assertEquals(expected, actual);
}
}
pom.xml
$ nano /opt/maven/hello-one/util/pom.xml
<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>
<!-- parent coordinates -->
<parent>
<groupId>com.xyz</groupId>
<artifactId>hello-one-pom</artifactId>
<version>1.0</version>
</parent>
<!-- project coordinates -->
<groupId>com.xyz</groupId>
<artifactId>util-jar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<!-- project dependencies -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
References: