d:\tools\jdk1.7.0_67\bin;D:\tools\apache-ant-1.8.4\bin;D:\tools\apache-maven-3.2.3\bin;
d:\tools\jdk1.7.0_67
md src md build\classes md build\jar
set PATH=%PATH%;d:\tools\jdk1.7.0_67\bin javac -sourcepath src -d build\classes src\oata\helloworld.java java -cp build\classes oata.helloworld
echo Main-Class: oata.helloworld>myManifest md build\jar jar cfm build\jar\helloworld.jar myManifest -C build\classes . java -jar build\jar\helloworld.jar
package oata; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class helloworld { static Logger logger = LogManager.getLogger(helloworld.class); public static void main(String[] args) { System.out.println("Hello World"); logger.error("Hello World"); } }
D:\projects\java\externaljar>javac -cp "lib\log4j-core-2.0.jar";"lib\log4j-api-2.0.jar" -sourcepath src -d build\classes src\oata\helloworld.java
java -cp "lib\log4j-core-2.0.jar";"lib\log4j-api-2.0.jar";"build\classes" oata.helloworld
refer:
Apache Ant is a Java-based build tool. In theory, it is kind of like make, without make's wrinkles.
Instead of writing shell commands, the configuration files are XML-based, calling out a target tree where various tasks get executed. Each task is run by an object that implements a particular Task interface. Below are list ant tasks:(refer http://ant.apache.org/manual/index.html at toc Ant Tasks)
<project> <target name="clean"> <delete dir="build"/> </target> <target name="compile"> <mkdir dir="build/classes"/> <javac srcdir="src" destdir="build/classes"/> </target> <target name="jar"> <mkdir dir="build/jar"/> <jar destfile="build/jar/helloworld.jar" basedir="build/classes"> <manifest> <attribute name="Main-Class" value="oata.helloworld"/> </manifest> </jar> </target> <target name="run"> <java jar="build/jar/helloworld.jar" fork="true"/> </target> </project>
⇒ list targets in build.xml:
clean, compile, jar, run
⇒ and list tasks in build.xml:
delete, mkdir, javac, jar, java
set PATH=%PATH%;D:\tools\apache-ant-1.8.4\bin;d:\tools\jdk1.7.0_67\bin ant compile ant jar ant run
<project name="helloworld" basedir="." default="main"> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="main-class" value="oata.helloworld"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/> </target> <target name="clean-build" depends="clean,jar"/> <target name="main" depends="clean,run"/> </project>
ant -buildfile enhance-build.xml
Output:
Buildfile: D:\projects\java\simpleapplication\enhance-build.xml clean: [delete] Deleting directory D:\projects\java\simpleapplication\build compile: [mkdir] Created dir: D:\projects\java\simpleapplication\build\classes [javac] D:\projects\java\simpleapplication\enhance-build.xml:12: warning: 'includeantruntime' was not set, defaultin g to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 1 source file to D:\projects\java\simpleapplication\build\classes jar: [mkdir] Created dir: D:\projects\java\simpleapplication\build\jar [jar] Building jar: D:\projects\java\simpleapplication\build\jar\helloworld.jar run: [java] Hello World main: BUILD SUCCESSFUL Total time: 1 second
Update helloworld.java for using log4j external library:
package oata; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class helloworld { static Logger logger = LogManager.getLogger(helloworld.class); public static void main(String[] args) { System.out.println("Hello World"); logger.error("Hello World"); } }
to use external lib log4j, we update the build.xml follow changes below: And full content of build.xml ater change:
<project name="helloworld" basedir="." default="main"> <property name="lib.dir" value="lib"/> <path id="classpath"> <fileset dir="${lib.dir}" includes="**/*.jar"/> </path> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="main-class" value="oata.helloworld"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java fork="true" classname="${main-class}"> <classpath> <path refid="classpath"/> <path location="${jar.dir}/${ant.project.name}.jar"/> </classpath> </java> </target> <target name="clean-build" depends="clean,jar"/> <target name="main" depends="clean,run"/> </project>
Go to properties → Java Build Path
refer:
Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project
Maven’s primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:
Maven will build all java classes in the src, and don't care the class names in src
Create basic simple maven:
set PATH=%PATH%;D:\tools\apache-maven-3.2.3\bin;D:\tools\jdk1.7.0_67\bin set JAVA_HOME=D:\tools\jdk1.7.0_67\ mvn --version
mvn archetype:generate -DgroupId=com.mycompany -DartifactId=simplemaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
And the maven will create the directory tree below:
simplemaven |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- mycompany | `-- App.java `-- test `-- java `-- com `-- mycompany `-- AppTest.java
cd simplemaven mvn compile
⇒ maven will download all dependency packages to ~/.m2
java -cp target\simplemaven-1.0-SNAPSHOT.jar com.mycompany.App
Output:
Hello World!
Create new class and run it in simple maven:
mvn compile
java -cp target\simplemaven-1.0-SNAPSHOT.jar com.mycompany.Helloworld
Output:
Hello World!
mvn -h
mvn compile
<project> ........ <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.mycompany.Helloworld</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ........ </project>
mvn compile java -jar target\simplemaven-1.0-SNAPSHOT.jar
refer:
Below are methods to include resources:
<project> ... <build> ... <resources> <resource> <directory>[your folder here]</directory> </resource> </resources> ... </build> ... </project>
<project> ... <name>My Resources Plugin Practice Project</name> ... <build> ... <resources> <resource> <directory>[your directory]</directory> <includes> <include>[resource file #1]</include> <include>[resource file #2]</include> <include>[resource file #3]</include> ... <include>[resource file #n]</include> </includes> </resource> ... </resources> ... </build> ... </project>
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.0</version> </dependency> </dependencies>
And below are full 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.mycompany</groupId> <artifactId>mavenexternaljar</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>mavenexternaljar</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.0</version> </dependency> </dependencies> </project>
package com.mycompany; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * Hello world! * */ public class App { static Logger logger = LogManager.getLogger(App.class); public static void main( String[] args ) { System.out.println( "Hello World!" ); logger.error("Hello World"); } }
mvn compile
mvn exec:java -Dexec.mainClass="com.mycompany.App"
refer: http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
mvn exec:java -Dexec.mainClass="com.mycompany.App"
mvn exec:java -Dexec.mainClass="com.mycompany.App" -Dexec.args="arg0 arg1 arg2"
mvn exec:java -Dexec.mainClass="com.mycompany.App" -Dexec.classpathScope=runtime
Although hardly a comprehensive list, these are the most common default lifecycle phases executed:
There are two other Maven lifecycles of note beyond the default list above. They are:
Base on mavent phases, we can run command below to clean all files which are generated by maven by prior builds:
mvn clean
refer: http://maven.apache.org/eclipse-plugin.html
Go to help→Install New Software: http://download.eclipse.org/technology/m2e/releases
M2E: The Maven Integration for Eclipse is the official Eclipse project aimed at integrating Maven within the Eclipse IDE. It is released under the EPL 1.0 license. Features include:
config maven source plugin in pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
config maven javadoc plugin in pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
run command below:
mvn wrap
refer: https://gradle.org/docs/
Introduce about Gradle:
Gradle supports the following repository formats:
Below are steps to install Gradle Plugin in Eclipse
gradle
refer: https://docs.gradle.org/3.3/userguide/dependency_management.html
<dependency> <groupId>com.badlogicgames.gdx</groupId> <artifactId>gdx</artifactId> <version>1.9.6</version> </dependency>
project(":core") { apply plugin: "java" dependencies { compile "com.badlogicgames.gdx:gdx:1.9.6" ............................... } }
Go to C:\Users\anhvc\.gradle and delete all files and directories in this directory ⇒ Fix error run gradle when you change java version from 1.8 to 1.7. When you change java version, you must reload new jar packages of gradle in new version 1.7
I 've tried installing Java 8, and setting my project to use Java 8. If I change my JAVA_HOME to 1.8 on the command line and compile with gradle it works. If I try to use JAVA_HOME set to 1.7 and gradle it fails with the same error.
java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
⇒ Fix: set JAVA_HOME and PATH to java8