Java vs Maven - hqzhang/cloudtestbed GitHub Wiki
when maven use nexus as local reposity, we need following settings:
0) nexus configuration
Configuring Nexus as a Maven repo
i)create/useexisted a private (hosted) repository: maven-snapshots(version policy)
http://localhost:8081/repository/maven-snapshots
ii) create/useexisted a private (hosted) repository: maven-releases (version policy)
http://localhost:8081/repository/maven-release/
iii)create a proxy repository to MavenCentral: maven-central(releases and proxy)
http://localhost:8081/repository/maven-central/
iv) create/useexisted a group repository that contain maven-central/release/snapshots
http://localhost:8081/repository/maven-public/ redirect to i, ii and iii.
1) cat ~/.m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers><server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>{q8TSMPJb4WQHXUcKoYCL73omIom95xx+Wg1snOEtURg=}</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>{q8TSMPJb4WQHXUcKoYCL73omIom95xx+Wg1snOEtURg=}</password>
</server> </servers>
<!--2) use nexus as repository rather than public-->
<mirrors><mirror>
<id>central</id>
<name>central</name>
<url>http://localhost:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror></mirors>
</settings>
3)if need encrypt password, require cat settings-security.xml
<settingsSecurity>
<master>{+Gtlg4r1aLkHSaMH5ANrzFe+ZlcJJEKAK9UPPVr8pys=}</master>
</settingsSecurity>
4) Encrytion command:
mvn --encrypt-master-password admin123 //Encrypt master security password
{+Gtlg4r1aLkHSaMH5ANrzFe+ZlcJJEKAK9UPPVr8pys=}
mvn --encrypt-password admin123. //encrypt server pass
{q8TSMPJb4WQHXUcKoYCL73omIom95xx+Wg1snOEtURg=}
5) push package define in pom.xml
<project>
<distributionManagement>
<repository>
<id>nexusdeploymentrepo</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexusdeploymentrepo</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
https://github.com/rchernanko/args4j-maven-example
<build>
<plugins>
<!-- for executing directly mvn exec:java -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>execution</id>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.mycompany.app.App</mainClass>
<arguments>
<argument>-name</argument>
<argument>${name}</argument>
<argument>-age</argument>
<argument>${age}</argument>
</arguments>
<!--Instead of arguments, I could've also used the commandLineArgs option too - see below-->
<!--But args is a little nicer to read-->
<!--<commandlineArgs>-name ${name} -age ${age}</commandlineArgs>-->
</configuration>
</plugin>
<!-- for copy extra files -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/imrgen</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- for build/compile purpose -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java.compiler.level}</source>
<target>${java.compiler.level}</target>
</configuration>
</plugin>
<!-- for create manifest file purpose -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- Adding MainClass on Manifest file -->
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>**/test/framework/**/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<!-- Including dependencies on jar -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
#Compare with Gcc and Java
1)compile
gcc -C main.c
javac -cp ./src/main/java ./src/main/java/com/exec/one/*.java ./src/main/java/com/exec/one/**/*.java -d ./out/
2) copy necessary files into ./out for java
3) Link or package
gcc -Wall main.c -o main -llib //-Idir for headerfiles . -Llibdir for search dir
jar jar cvfm ExecutableOne.jar src/main/resources/META-INF/MANIFEST.MF -C ./out/ .
#Play Java using CLI
1. Create Project with pom.xml
0)mvn archetype:generate -DgroupId=com.exec.one \
-DartifactId=my-main \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
1).add App.java
package com.exec.one;
import com.exec.one.service.MagicService;
public class App {
public static void main(String[] args){
System.out.println("Main Class Start");
MagicService service = new MagicService();
System.out.println("MESSAGE : " + service.getMessage());
}
}
2) add service/MagicService.java
package com.exec.one.service;
public class MagicService {
private final String message;
public MagicService(){
this.message = "Magic Message";
}
public String getMessage(){
return message;
}
}
3) add manifest.mf
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.exec.one.Main
2. Compile the Code and pacakge and run
1) compile
javac -cp ./src/main/java ./src/main/java/com/exec/one/*.java ./src/main/java/com/exec/one/**/*.java -d ./out/
2) copy necessary files into ./out
3) tar a package
jar cvfm ExecutableOne.jar src/main/resources/META-INF/MANIFEST.MF -C ./out/ .
4) run/execute
java -jar ExecutableOne.jar
3. Use the Jar as Libray
0)mvn archetype:generate -DgroupId=com.exec.two \
-DartifactId=my-main \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
1) add ExecutableOne.jar under out/libs
2) add com.exec.two.App.java
package com.exec.two;
import com.exec.one.service.MagicService;
import com.google.common.hash.Hashing;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args){
System.out.println("Executable-Two Main");
MagicService service = new MagicService();
System.out.println("MagicService from Executable-ONE");
System.out.println("MESSAGE: " + service.getMessage());
String ssn="999-99-999";
String dob="02/03/1966";
System.out.println(generateHashed(ssn,dob));
}
public static String generateHashed(String ssn, String dob) {
String sum=ssn+dob;
final String hash = Hashing.sha256()
.hashString(sum, StandardCharsets.UTF_8).toString();
return hash;
}
}
3) add manifest.mf
Manifest-Version: 1.0
Class-Path: libs/ExecutableOne.jar libs/guava-20.0.jar
Main-Class: com.exec.two.App
4. Compile , package and run the code
1) compile
mkdir out
javac -cp ./src/main/java:./libs/* ./src/main/java/com/exec/two/*.java -d ./out/
2) package
cp -r libs out/
jar cvfm ExecutableTwo.jar ./src/main/resources/META-INF/MANIFEST.MF -C ./out/ .
3) execute
java -jar ExecutableTwo.jar
#Using Maven to do the same thing
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localdomain.localhost.tutorial</groupId>
<artifactId>java-archive</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<commons-lang3.version>3.7</commons-lang3.version>
<lombok.version>1.16.20</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Google Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<!-- Jar file entry point -->
<mainClass>com.mycompany.api.utilities.sharedUtils</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>