Skip to content

Instantly share code, notes, and snippets.

@RajaniCode
Last active February 21, 2025 20:16
Show Gist options
  • Select an option

  • Save RajaniCode/0f7d58b25908508d272d8343eaec123f to your computer and use it in GitHub Desktop.

Select an option

Save RajaniCode/0f7d58b25908508d272d8343eaec123f to your computer and use it in GitHub Desktop.
Maven Java
###########################################################################################################################
# Maven # Java
###########################################################################################################################
****************************************************************************************************************************
% export JAVA_HOME="$HOME/Downloads/Software/OpenJDK/JDK23.0.2/jdk-23.0.2.jdk/Contents/Home"
% export M2_HOME="$HOME/Downloads/Software/ApacheMaven/apache-maven-3.9.9"
% export M2="$M2_HOME/bin"
% export PATH=$PATH:"$M2:$JAVA_HOME/bin"
[
% export MAVEN_OPTS="-Xms256m -Xmx512m --enable-preview"
]
% export MAVEN_OPTS="-Xms256m -Xmx512m"
# Or
% export PATH="$HOME/Downloads/Software/OpenJDK/JDK23.0.1/jdk-23.0.1.jdk/Contents/Home/bin/:$HOME/Downloads/Software/ApacheMaven/apache-maven-3.9.9/bin/":$PATH
% java --version
% mvn --version
****************************************************************************************************************************
1. mvn archetype
***************************************************************************************************************************
# mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
# NB: Directory should be empty
% mvn archetype:generate -DgroupId="com.commons.project" -DartifactId=sample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
***************************************************************************************************************************
# mvn archetype # default # % tree
***************************************************************************************************************************
.
└── sample
├── pom.xml
└── src
├── main
│   └── java
│   └── com
│   └── commons
│   └── project
│   └── App.java
└── test
└── java
└── com
└── commons
└── project
└── AppTest.java
13 directories, 3 files
***************************************************************************************************************************
# mvn archetype # default # % cat sample/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>
<groupId>com.commons.project</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
***************************************************************************************************************************
# mvn archetype # default # % cat sample/src/main/java/com/commons/project/App.java
***************************************************************************************************************************
package com.commons.project;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
***************************************************************************************************************************
# mvn archetype # default # % cat sample/src/test/java/com/commons/project/AppTest.java
***************************************************************************************************************************
package com.commons.project;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
***************************************************************************************************************************
2. Edit sample/pom.xml
***************************************************************************************************************************
===========================================================================================================================
# Add # properties
===========================================================================================================================
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<argLine>--enable-preview</argLine>
</properties>
===========================================================================================================================
# Update # JUnit
===========================================================================================================================
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
===========================================================================================================================
# Add # maven-compiler-plugin # release # --enable-preview
===========================================================================================================================
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>${java.version}</release>
<release>23</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
===========================================================================================================================
# Add # maven-jxr-plugin
===========================================================================================================================
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.6.0</version>
<reportSets>
<reportSet>
<reports>
<report>jxr-no-fork</report>
<report>test-jxr-no-fork</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
===========================================================================================================================
***************************************************************************************************************************
# Edited pom.xml # % cat sample/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>
<groupId>com.commons.project</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<argLine>--enable-preview</argLine>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>${java.version}</release>
<release>23</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.6.0</version>
<reportSets>
<reportSet>
<reports>
<report>jxr-no-fork</report>
<report>test-jxr-no-fork</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
***************************************************************************************************************************
3. Edit(ed) App.java # % cat sample/src/main/java/com/commons/project/App.java
***************************************************************************************************************************
package com.commons.project;
import java.util.stream.Stream;
/**
* App main
*
*/
public class App {
private Stream<Integer> fibonacci = Stream.iterate(new int[]{0, 1}, f -> new int[]{f[1], f[0] + f[1]}).map(f -> f[0]);
public static void main( String[] args ) {
App instance = new App();
instance.printSystemProperties();
int fibonacciNumber = 46;
System.out.println(String.format("Fibonacci Calculator for %d is %s", fibonacciNumber, instance.fib(fibonacciNumber)));
}
public void printSystemProperties() {
System.out.println(String.format("OS Name: %s", System.getProperty("os.name")));
System.out.println(String.format("OS Version: %s", System.getProperty("os.version")));
System.out.println(String.format("OS Architecture: %s", System.getProperty("os.arch")));
System.out.println(String.format("Java Version: %s", System.getProperty("java.version")));
System.out.println(String.format("Java Vendor Version: %s", System.getProperty("java.vendor.version")));
System.out.println(String.format("Java VM Name: %s", System.getProperty("java.vm.name")));
// System.getProperties().list(System.out);
}
public int fib(int fibonacciNumber) {
if (fibonacciNumber < 0 || fibonacciNumber > 46) {
throw new IllegalArgumentException("Number should be an integer >= 0 & <= 46");
}
return fibonacci.skip(fibonacciNumber).findFirst().orElse(0);
}
}
***************************************************************************************************************************
4. Edit(ed) AppTest.java # % cat sample/src/test/java/com/commons/project/AppTest.java
***************************************************************************************************************************
package com.commons.project;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
private final App instance = new App();
@Test
void assertTest() {
instance.printSystemProperties();
assert(true);
}
@Test
void fibonacciTest() {
assertEquals(1836311903, instance.fib(46));
}
@Test
void exceptionTest() {
Exception exception = assertThrows(IllegalArgumentException.class, () ->
instance.fib(47));
assertEquals("Number should be an integer >= 0 & <= 46", exception.getMessage());
}
}
***************************************************************************************************************************
5. mvn package
***************************************************************************************************************************
% cd sample
[
# mvn validate - validate the project is correct and all necessary information is available
# mvn compile - compile the source code of the project
# mvn test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
# mvn package - take the compiled code and package it in its distributable format, such as a JAR.
# mvn verify - run any checks on results of integration tests to ensure quality criteria are met
# mvn install - install the package into the local repository, for use as a dependency in other projects locally
# mvn deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
]
% mvn dependency:tree
% mvn package
***************************************************************************************************************************
6. mvn test
***************************************************************************************************************************
% mvn test
% mvn -Dtest=AppTest test
% mvn -Dtest=AppTest#assertTest test
% mvn -Dtest=AppTest#fibonacciTest test
% mvn -Dtest=AppTest#exceptionTest test
***************************************************************************************************************************
7. mvn # Surefire Report
***************************************************************************************************************************
% mvn surefire-report:report
% jwebserver --version
% jwebserver --directory $(pwd)/target/reports --port 8000
# Terminal Window # curl # open
% curl http://127.0.0.1:8000/surefire.html
% open http://127.0.0.1:8000/surefire.html
***************************************************************************************************************************
8. mvn java
***************************************************************************************************************************
# Original Terminal Window
<control + c>
# NB
[
# symbolic reference class should be accessible
# public class App
# preview
% export MAVEN_OPTS="-Xms256m -Xmx512m --enable-preview"
]
% mvn exec:java -Dexec.mainClass=com.commons.project.App
***************************************************************************************************************************
###########################################################################################################################
###########################################################################################################################
# Maven Wrapper # Java
###########################################################################################################################
***************************************************************************************************************************
1. mvn wrapper
***************************************************************************************************************************
[
% mvn clean
]
# Setup the Maven Wrapper using the Maven Wrapper Plugin with its provided wrapper goal
% mvn wrapper:wrapper
# For Maven, install a specific version of Apache Maven, put it on the PATH and then run the mvn command
[
% mvn clean install
]
% For Maven Wrapper, run wrapper scripts
% ./mvnw clean install
# Windows
[
> mvnw.cmd clean install
]
***************************************************************************************************************************
2. mvnw test
***************************************************************************************************************************
% ./mvnw test
% ./mvnw -Dtest=AppTest test
% ./mvnw -Dtest=AppTest#assertTest test
% ./mvnw -Dtest=AppTest#fibonacciTest test
% ./mvnw -Dtest=AppTest#exceptionTest test
***************************************************************************************************************************
3. mvnw # Surefire Report
***************************************************************************************************************************
% ./mvnw surefire-report:report
% jwebserver --version
% jwebserver --directory $(pwd)/target/reports --port 8000
# Terminal Window # curl # open
% curl http://127.0.0.1:8000/surefire.html
% open http://127.0.0.1:8000/surefire.html
***************************************************************************************************************************
4. mvnw java
***************************************************************************************************************************
# Original Terminal Window
<control + c>
# NB
[
# symbolic reference class should be accessible
# public class App
# preview
% export MAVEN_OPTS="-Xms256m -Xmx512m --enable-preview"
]
% ./mvnw exec:java -Dexec.mainClass=com.commons.project.App
***************************************************************************************************************************
###########################################################################################################################
###########################################################################################################################
***************************************************************************************************************************
# jar
***************************************************************************************************************************
% jar tvf target/sample-1.0-SNAPSHOT.jar
***************************************************************************************************************************
# java
***************************************************************************************************************************
# NB
[
# symbolic reference class should be accessible
# public class App
# preview
% export MAVEN_OPTS="-Xms256m -Xmx512m --enable-preview"
% java --enable-preview -classpath target/sample-1.0-SNAPSHOT.jar com.commons.project.App
]
% java -classpath target/sample-1.0-SNAPSHOT.jar com.commons.project.App
***************************************************************************************************************************
###########################################################################################################################
// Credits
/*
https://openjdk.org/
https://java.com/
https://maven.apache.org/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment