このチュートリアルでは、https://maven.apache.org/[Maven]を使用してJavaプロジェクトを管理する方法を示します.Javaプロジェクトを作成し、依存関係を追加し、Javaプロジェクトを実行可能なjarファイルにパッケージ化します。最後に、SHA-256アルゴリズムで指定された文字列をハッシュする実行可能なjarファイルを作成します。
使用される技術:
-
Maven 3.5.3
-
JDK 8
-
Apache Commonsコーデック1.11
1. Mavenテンプレートからプロジェクトを作成する
端末(** uixまたはMac)またはコマンドプロンプト(Windows)で、Javaプロジェクトを作成するフォルダに移動します。次のコマンドを入力します。
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId={maven-template} -DinteractiveMode=false
これは、MavenテンプレートからJavaプロジェクトを生成するようにMavenに指示します。例えば、
D:\>mvn archetype:generate -DgroupId=com.mkyong.hashing -DartifactId=java-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ... [INFO]------------------------------------------------------------------------ [INFO]BUILD SUCCESS [INFO]------------------------------------------------------------------------ [INFO]Total time: 3.992 s [INFO]Finished at: 2018-09-27T17:15:57+08:00 [INFO]------------------------------------------------------------------------
Above command will generate a Java project from
maven-archetype-quickstart
template.
2. Maven Directory Layout
The following project directory structure will be created. In short,
source code puts in folder
/src/main/java/
, unit test code puts in
/src/test/java/
.
P.S Above figure is captured from IntelliJ IDEA, just ignore those
.idea
folder.
3. POM file
Review the generated
pom.xml
. It’s quite empty, just a single jUnit
dependency.
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.mkyong.hashing </groupId> <artifactId> java-project3 </artifactId> <packaging> jar </packaging> <version> 1.0-SNAPSHOT </version> <name> java-project </name> <url> http://maven.apache.org </url> <依存関係> <依存関係> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 3.8.1 </version> <scope> test </scope> </dependency> </dependencies> </project>
このPOMファイルは、Antの `build.xml`ファイルと似ています。
プロジェクト情報、ディレクトリ構造、プロジェクトのすべて
プラグイン、プロジェクトの依存関係、このプロジェクトのビルド方法など
この
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html;infial
POMガイド]。
4. POMを更新する
4.1 Mavenに指定されたJDKバージョンを使用するようにコンパイラプロパティを追加する
ソースコードをコンパイルします。
<プロパティ> <! - 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>
4.2 jUnitを4.12に更新する
<依存関係> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 4.12 </version> <scope> test </scope> </dependency>
4.3 SHAハッシュのために `commons-codec`を追加します。
<! - ハッシュの依存関係 - > <! - https://search.maven.org/artifact/commons-codec/commons-codec/1.11/jar - > <依存関係> <groupId>コモンズコーデック</groupId> <artifactId>コモンズコーデック</artifactId> <version> 1.11 </version> </dependency>
4.4完全な更新版。
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.mkyong.hashing </groupId > <artifactId> java-project </artifactId> <パッケージ> jar </パッケージ> <バージョン> 1.0-SNAPSHOT </バージョン> <名前> java-project </名前> <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> <dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 4.12 </version> <scope>テスト</scope> </dependency> <dependency> <groupId>コーデック</groupId> <artifactId>コモンズコーデック</artifactId> <version> 1.11 </version> </dependency> </dependencies> </project>
5.コードを書く
5.1 `App.java`を更新して入力を受け取り、SHA-256でハッシュします
アルゴリズム。
App.java
パッケージcom.mkyong.hashing; org.apache.commons.codec.digest.DigestUtilsをインポートします。 パブリッククラスApp { public static void main(String[]args){ if(args.length <1){System.err.println( "入力してください!"); System.exit(0); } System.out.println(sha256hex(args[0])); } public static String sha256hex(文字列入力){戻り値DigestUtils.sha256Hex(入力); } }
5.2ユニットテスト。
AppTest.java
パッケージcom.mkyong.hashing; インポートorg.junit.Assert; import org.junit.Test; パブリッククラスAppTest { プライベート文字列INPUT = "123456"; @Test public void testLength(){Assert.assertEquals(64、App.sha256hex(INPUT).length()); } @Test public void testHex(){String expected = "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"; Assert.assertEquals(予想、App.sha256hex(INPUT)); } }
完了しました。
6. Mavenビルド
6.1 `mvnパッケージ`でビルドしましょう
D:\ java-project> mvnパッケージ[情報]プロジェクトのスキャン中... [INFO] [INFO]------------------< com.mkyong.hashing:java-project >------------------- [INFO]Building java-project 1.0-SNAPSHOT [INFO]--------------------------------[jar]--------------------------------- [INFO] ...... ------------------------------------------------------- T E S T S S -------------------------------------------------- ----- com.mkyong.hashing.AppTestの実行 テスト実行:2、失敗:0、エラー:0、スキップ:0、経過時間:0.067秒 結果 : テスト実行:2、失敗:0、エラー:0、スキップ:0 [情報][情報]--- maven-jar-plugin:2.4:jar(デフォルト - jar)@ java-project ---[情報]ビルドジャー:D:\ java-project \ target \ java-project-1.0-SNAPSHOT.jar[情報]----------------------------------------------- -------------------------[情報]ビルドの成功[情報]----------------------------------------------- -------------------------[情報]合計時間:1.956秒[情報]終了:2018-09-28T12:40:18 08:00[情報]----------------------------------------------- -------------------------
コンパイルし、ユニットテストを実行し、プロジェクトを
jar`ファイルにパッケージ化し、それを
project/target`フォルダに入れます。
7.#1を実行する
7.1実行します。 Oops …デフォルトでは、Mavenはプロジェクトの依存関係を `commons-codec`をjarファイルに追加しませんでした。
D:\java-project>java -cp target/java-project-1.0-SNAPSHOT.jar com.mkyong.hashing.App 123456 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/digest/DigestUtils at com.mkyong.hashing.App.sha256hex(App.java:18) at com.mkyong.hashing.App.main(App.java:13) Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.digest.DigestUtils at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more
7.2これを解決するために、この `maven-shade-plugin`を使用して、uber/fat-jarを作成します。すべてを単一のjarファイルにまとめます。
pom.xml
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.0</version> <executions> <!-- Attach the shade goal into the package phase --> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
7.3もう一度パッケージ化!
D:\java-project>mvn clean package[INFO]Scanning for projects...[... [INFO]--- maven-shade-plugin:3.2.0:shade (default) @ java-project ---[INFO]Including commons-codec:commons-codec:jar:1.11 in the shaded jar.[INFO]Replacing original artifact with shaded artifact. [INFO]Replacing D:\java-project\target\java-project-1.0-SNAPSHOT.jar with D:\java-project\target\java-project-1.0-SNAPSHOT-shaded.jar ...
Two jars will be generated, review the file size :
D:¥java-project> dir targetドライブDのボリュームはSamsung970ボリュームシリアル番号は10DF-E63D D:\ java-project \ targetのディレクトリ 28/09/2018 12:57 PM 335,643 java-project-1.0-SNAPSHOT.jar 28/09/2018 12:57 PM 3,053元java-project-1.0-SNAPSHOT.jar ...
8.#2を実行する
8.1もう一度実行してください。良い、結果は期待される。
D:\java-project>java -cp target/java-project-1.0-SNAPSHOT.jar com.mkyong.hashing.App 123456 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
8.2 Jarとして実行できますか?いいえ、メインクラスはありません。
D:\java-project>java -jar target/java-project-1.0-SNAPSHOT.jar 123456 no main manifest attribute, in target/java-project-1.0-SNAPSHOT.jar
8.3それを解決するには、メインクラスをこのように `maven-shade-plugin`に追加してください。
pom.xml
<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>
9.#3を実行する
9.1もう一度パッケージ化!
D:\java-project>mvn clean package
9.2 Jarファイルとして実行します。
D:\java-project>java -jar target/java-project-1.0-SNAPSHOT.jar 123456 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
完了しました。
10. POM
最終的なPOMファイル。
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.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> </plugins> </build> </project>
ソースコードをダウンロードする
$ gitクローンhttps://github.com/mkyong/maven-examples.git $ cd java-project $ mvnパッケージ$ java -jar target/java-project-1.0-SNAPSHOT.jar 123456
参考文献
-
リンク://maven/how-to-create-a-web-application-with-maven/[Maven
-
Java Webアプリケーションプロジェクトの作成方法]。
Apache Mavenプロジェクト
-
-
https://books.sonatype.com/mvnex-book/reference/simple-project.html
[A
シンプルMavenプロジェクト]。リンク://java/java-sha-hashing-example/[Java SHAハッシングの例]
-
link://maven/how-to-create-a-jar-file-with-maven/[Jarを作成する方法
ファイル、シンJarの例]。 link://maven/maven-create-a-fat-jar-file-one-jar-example/[fatを作成する
JARファイル – One-JARの例]。 link://maven/create-a-fat-jar-file-maven-shade-plugin/[脂肪を作成する
Jarファイル – Maven Shade Pluginの例]