Using MAVEN to package Java programs-with main class, with dependent "go"

Source: Internet
Author: User

Most of the time, we need to package the program we're writing, and at this point we can build tools like Maven, SBT, Ant, and so on, where I'm using maven.

Packaged into a jar package that can have a main class (no dependencies in the jar package)

The following is a jar package that is packaged as executable with the main class:

<Project> ...<Build><Plugins><Plugin><Groupid>org.apache.maven.plugins</Groupid><Artifactid>maven-jar-plugin</Artifactid> ...<Configuration><Archive><manifest> <addclasspath>true</addclasspath> <mainclass> Fully.qualified.MainClass</mainclass> <!--your main class name-- </manifest> < /archive> </configuration> ... </plugin> </plugins> </build>     

But, although the program is now packaged into a jar file can also run, but the jar is not included in the dependency, so if the program has other dependencies, then in the run of this program, you need to specify the class directory, and to put all the dependencies in the class directory, manually add dependencies to the class directory , it's a nightmare ...

I have done so, it is less a dependency, I will manually add a to the class directory, less one, plus one, less one, plus one, when I added about 10 a few after, but also do not see the end ... Simply to crazy rhythm, and then remembered before with SBT packaging, but also can add dependencies to jar package, mood suddenly improved, hurriedly Google, so find Google to Maven's official website .... By the way, although we Google, we will search for a lot of results, but many times, we better choose the right results to read, my general Order is: official priority, Stackexchange ...

Packaged with a dependent jar package configuration without a main class and a dependent jar package

Edit the Pom.xml file with the following content:

<project> [...]<build> [...]<Plugins><Plugin><Artifactid>maven-assembly-plugin</Artifactid><version>3.0.0</Version><Configuration><Descriptorrefs><Descriptorref>jar-with-dependencies</Descriptorref></Descriptorrefs></Configuration><Executions><Execution><id>make-assembly</id> <!--This was used for inheritance merges--<phase>package</ phase> <!--bind to the packaging phase--<goals> <goal> Single</goal> </ goals> </execution> </executions> </ plugin> [...] </PROJECT>         

Add main class also with dependent jar package

Edit the Pom.xml file with the following content:

<Project><Build><Plugins><!--(start) for package jar with dependencies--<Plugin><Artifactid>maven-assembly-plugin</Artifactid><version>3.0.0</Version><Configuration><Archive><Manifest><Mainclass>main</Mainclass></Manifest></Archive><Descriptorrefs><Descriptorref>jar-with-dependencies</Descriptorref></Descriptorrefs></Configuration><Executions><Execution><id>make-assembly</Id><!--this was used for inheritance merges--<Phase>package</phase> <!--bind to the packaging phase--<goals> <goal> Single</goal> </ goals> </execution> </executions> </ plugin> <!--(end) for package jar with dependencies-- </plugins> </build></PROJECT>        

It is important to note that you do not modify the Pom.xml file as follows, although there is no error, but MANIFEST.MF there is no main class in the generated jar package, such a jar package cannot be run directly.

<project> [...]<build> [...]<Plugins><Plugin><Groupid>org.apache.maven.plugins</Groupid><Artifactid>maven-jar-plugin</Artifactid> ...<Configuration><Archive><Manifest><Addclasspath>true</Addclasspath><Mainclass>fully.qualified.mainclass</Mainclass><!--your main class name--</Manifest></Archive></Configuration> ...</Plugin><Plugin><Artifactid>maven-assembly-plugin</Artifactid><version>3.0.0</Version><Configuration><Descriptorrefs><Descriptorref>jar-with-dependencies</Descriptorref></Descriptorrefs></Configuration><Executions><Execution><id>make-assembly</id> <!--This was used for inheritance merges--<phase>package</ phase> <!--bind to the packaging phase--<goals> <goal> Single</goal> </ goals> </execution> </executions> </ plugin> [...] </PROJECT>         

Packaged

Use the following command to package:

package

As the official website says, "This is pretty simple! “


However, when I run the jar package that operates HDFS, I have the following issues:

ExceptionIn thread"Main" Java, I-. Ioexception:no FileSystem for Scheme:hdfs at org. Apache. Hadoop. FS. FileSystem. Getfilesystemclass (FileSystem. Java:2660) at org. Apache. Hadoop. FS. FileSystem. Createfilesystem (FileSystem. Java:2667) at org. Apache. Hadoop. FS. FileSystem. access$200 (FileSystem. Java:94) at Org. Apache. Hadoop. FS. Filesystem$cache. getinternal (FileSystem. Java:2703) at org. Apache. Hadoop. FS. Filesystem$cache. Get (FileSystem. Java:2685) at org. Apache. Hadoop.FS. FileSystem. Get (FileSystem. Java:373) at Sentry. Magic. FS.dfs.<init> (Dfs. Java: ) at Sentry. Magic. FS. Checkflumeiscompleted.<init> (checkflumeiscompleted. Java:) at Main. Main (main. Java:                     (+)

Fortunately, a man [2] also encountered this problem, which makes the problem solved. Here's how to fix it:

    null;    Configuration conf = new Configuration();    public Dfs(URI uri) throws IOException {        // 必须有下面这一句 conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem"); // 必须有上面那一句 dfs = FileSystem.get(uri,conf); }

In addition to his methods, I have also found a way, through practice, is also feasible, my method is as follows:
Find the packaged jar package, then use the Archive Manager to open the jar package, "Open with Archive Manager", locate the META-INFO/services directory, locate the file in this directory, org.apache.Hadoop.fs.FileSystem edit the file, and add a line to the end of the file:

org.apache.hadoop.hdfs.DistributedFileSystem

However, it is not appropriate to modify the jar in such a way that it needs to be modified once per build, so it is recommended to use the previous workaround temporarily.

Reference documents:

[1] https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
[2] Http://www.cnblogs.com/justinzhang/p/4983673.html

Using MAVEN to package Java programs-with main class, with dependent "go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.