Forked from simonwoo/How to Create Build Java Project including all Dependencies Using Maven.md
Created
October 16, 2018 10:00
-
-
Save radut/89aa88f5728b923c3761f7cae115d71d to your computer and use it in GitHub Desktop.
Revisions
-
simonwoo revised this gist
Apr 22, 2015 . 1 changed file with 39 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -161,3 +161,42 @@ maven-jar-plugin: This plugin provides the capability to build and sign jars. </build> ``` You can extracts and review the content of MANIFEST.MF, the dependencies are added in the Class-Path. an exemple from flyway: ``` <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-license</id> <goals> <goal>copy-resources</goal> </goals> <phase>generate-resources</phase> <configuration> <resources> <resource> <directory>..</directory> <includes> <include>LICENSE.txt</include> <include>README.txt</include> </includes> </resource> </resources> <outputDirectory>${project.build.outputDirectory}/META-INF</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile> </archive> </configuration> </plugin> .... </plugins> ``` -
simonwoo renamed this gist
Apr 22, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
simonwoo revised this gist
Apr 22, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ There are 3 plugins for generating to create a JAR-File in maven: - maven-assembly-plugin - maven-shade-plugin maven-jar-plugin:This plugin provides the capability to build and sign jars.But it just compiles the java files under **src/main/java** and **/src/main/resources/**.It doesn't include the dependencies JAR files. ``` <!--exclude all xml files from the jar--> <plugin> -
simonwoo revised this gist
Apr 22, 2015 . 1 changed file with 88 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -63,7 +63,7 @@ maven-shade-plugin:it packages all dependencies into one uber-jar. It can also b <transformers> <!-- add Main-Class to manifest file --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>YourMainClass</mainClass> </transformer> </transformers> </configuration> @@ -72,5 +72,92 @@ maven-shade-plugin:it packages all dependencies into one uber-jar. It can also b </plugin> </plugins> ``` another solution-use some other plugins: - maven-resources-plugin - maven-dependency-plugin - maven-jar-plugin maven-resources-plugin: The Resources Plugin handles the copying of project resources to the output directory. The main resources are the resources associated to the main source code. maven-dependency-plugin: The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location. maven-jar-plugin: This plugin provides the capability to build and sign jars. ``` <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/ProjetName/</outputDirectory> <resources> <resource> <directory>resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <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}/ProjetName/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> <mainClass>ProjetMainClass</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> <finalName>Crunchify/Crunchify</finalName> </configuration> </plugin> </plugins> </build> ``` You can extracts and review the content of MANIFEST.MF, the dependencies are added in the Class-Path. -
simonwoo revised this gist
Apr 22, 2015 . 1 changed file with 73 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,76 @@ There are 3 plugins for generating to create a JAR-File in maven: - maven-jar-plugin - maven-assembly-plugin - maven-shade-plugin maven-jar-plugin:This plugin provides the capability to build and sign jars.But it just compiles the java files under **src/main/java** and **/src/main/resources/**.It doesn't include the dependencies. ``` <!--exclude all xml files from the jar--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <excludes> <exclude>**/*.xml</exclude> </excludes> </configuration> </plugin> ``` maven-assembly-plugin: this plugin extracts all dependency jars into raw classes, and group it together.It works in project with less dependencies only, for large project with many dependencies, it will cause Java class name conflict issue. ``` .... <build> <plugins> <!-- any other plugins --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> .... ``` maven-shade-plugin:it packages all dependencies into one uber-jar. It can also be used to build an executable jar by specifying the main class.this plugin is particularly useful as it merges content of specific files instead of overwriting them. This is needed when there are resource files that are have the same name across the jars and the plugin tries to package all the resource files. ``` <plugins> <!-- any other plugins --> <!-- Maven Shade Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <!-- Run shade goal on package phase --> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <!-- add Main-Class to manifest file --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.mkyong.core.utils.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> ``` -
simonwoo created this gist
Apr 22, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ There are 3 plugins for generating to create a JAR-File in maven: - maven-jar-plugin - maven-assembly-plugin - maven-shade-plugin