Commit 776dda80 authored by Muhammad Tehami's avatar Muhammad Tehami 💡

Initial commit

parents
# Maven – How to create a Java project
Maven 3, Apache Commons Codec, a simple Java project to hash a string with the SHA-256 algorithm.
Project Link - https://www.mkyong.com/maven/how-to-create-a-java-project-with-maven/
## How to run this project?
```
$ git clone https://github.com/mkyong/maven-examples.git
$ cd java-project
$ mvn package
$ java -jar target/java-project-1.0-SNAPSHOT.jar 123456
```
Output
```
8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
```
\ No newline at end of file
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.hashing</groupId>
<artifactId>java-project</artifactId>
<name>java-project</name>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>com.mkyong.hashing.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
<?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>
<groupId>com.mkyong.hashing</groupId>
<artifactId>java-project</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>java-project</name>
<url>http://maven.apache.org</url>
<properties>
<!-- https://maven.apache.org/general.html#encoding-warning -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Attach the shade into the package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mkyong.hashing.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- fat-jar -->
<!-- https://books.sonatype.com/mvnex-book/reference/customizing-sect-custom-packaged.html -->
<!--
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.mkyong.hashing.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>simple-command</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
</project>
package com.mkyong.hashing;
import org.apache.commons.codec.digest.DigestUtils;
public class App {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Please provide an input!");
System.exit(0);
}
System.out.println(sha256hex(args[0]));
}
public static String sha256hex(String input) {
return DigestUtils.sha256Hex(input);
}
}
package com.mkyong.hashing;
import org.junit.Assert;
import org.junit.Test;
public class AppTest {
private String INPUT = "123456";
@Test
public void testLength() {
Assert.assertEquals(64, App.sha256hex(INPUT).length());
}
@Test
public void testHex() {
String expected = "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92";
Assert.assertEquals(expected, App.sha256hex(INPUT));
}
}
\ No newline at end of file
/home/tehami/Training/maven-examples/java-project/src/main/java/com/mkyong/hashing/App.java
/home/tehami/Training/maven-examples/java-project/src/test/java/com/mkyong/hashing/AppTest.java
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite tests="2" failures="0" name="com.mkyong.hashing.AppTest" time="0.024" errors="0" skipped="0">
<properties>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="sun.boot.library.path" value="/usr/lib/jvm/jdk1.8.0_231/jre/lib/amd64"/>
<property name="java.vm.version" value="25.231-b11"/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
<property name="maven.multiModuleProjectDirectory" value="/home/tehami/Training/maven-examples/java-project"/>
<property name="java.vendor.url" value="http://java.oracle.com/"/>
<property name="path.separator" value=":"/>
<property name="guice.disable.misplaced.annotation.check" value="true"/>
<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
<property name="file.encoding.pkg" value="sun.io"/>
<property name="user.country" value="US"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.os.patch.level" value="unknown"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="user.dir" value="/home/tehami/Training/maven-examples/java-project"/>
<property name="java.runtime.version" value="1.8.0_231-b11"/>
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
<property name="java.endorsed.dirs" value="/usr/lib/jvm/jdk1.8.0_231/jre/lib/endorsed"/>
<property name="os.arch" value="amd64"/>
<property name="java.io.tmpdir" value="/tmp"/>
<property name="line.separator" value="
"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="os.name" value="Linux"/>
<property name="classworlds.conf" value="/home/tehami/.sdkman/candidates/maven/current/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib"/>
<property name="maven.conf" value="/home/tehami/.sdkman/candidates/maven/current/conf"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.class.version" value="52.0"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="os.version" value="5.0.0-37-generic"/>
<property name="library.jansi.path" value="/home/tehami/.sdkman/candidates/maven/current/lib/jansi-native"/>
<property name="user.home" value="/home/tehami"/>
<property name="user.timezone" value="Asia/Karachi"/>
<property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/>
<property name="java.specification.version" value="1.8"/>
<property name="file.encoding" value="UTF-8"/>
<property name="user.name" value="tehami"/>
<property name="java.class.path" value="/home/tehami/.sdkman/candidates/maven/current/boot/plexus-classworlds-2.6.0.jar"/>
<property name="java.vm.specification.version" value="1.8"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.home" value="/usr/lib/jvm/jdk1.8.0_231/jre"/>
<property name="sun.java.command" value="org.codehaus.plexus.classworlds.launcher.Launcher test"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
<property name="user.language" value="en"/>
<property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
<property name="java.vm.info" value="mixed mode"/>
<property name="java.version" value="1.8.0_231"/>
<property name="java.ext.dirs" value="/usr/lib/jvm/jdk1.8.0_231/jre/lib/ext:/usr/java/packages/lib/ext"/>
<property name="securerandom.source" value="file:/dev/./urandom"/>
<property name="sun.boot.class.path" value="/usr/lib/jvm/jdk1.8.0_231/jre/lib/resources.jar:/usr/lib/jvm/jdk1.8.0_231/jre/lib/rt.jar:/usr/lib/jvm/jdk1.8.0_231/jre/lib/sunrsasign.jar:/usr/lib/jvm/jdk1.8.0_231/jre/lib/jsse.jar:/usr/lib/jvm/jdk1.8.0_231/jre/lib/jce.jar:/usr/lib/jvm/jdk1.8.0_231/jre/lib/charsets.jar:/usr/lib/jvm/jdk1.8.0_231/jre/lib/jfr.jar:/usr/lib/jvm/jdk1.8.0_231/jre/classes"/>
<property name="java.vendor" value="Oracle Corporation"/>
<property name="maven.home" value="/home/tehami/.sdkman/candidates/maven/current"/>
<property name="file.separator" value="/"/>
<property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/>
<property name="sun.cpu.endian" value="little"/>
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="sun.desktop" value="gnome"/>
<property name="sun.cpu.isalist" value=""/>
</properties>
<testcase classname="com.mkyong.hashing.AppTest" name="testHex" time="0.024"/>
<testcase classname="com.mkyong.hashing.AppTest" name="testLength" time="0"/>
</testsuite>
\ No newline at end of file
-------------------------------------------------------------------------------
Test set: com.mkyong.hashing.AppTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.081 sec
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment