使用Maven對JAVA程式打包-帶主類、帶依賴【轉】

來源:互聯網
上載者:User

標籤:知識庫   ted   arc   version   classpath   html   xml檔案   make   googl   

很多時候,我們需要對編寫的程式進行打包,這個時候,我們可以藉助一些項目構建工具,如maven, sbt, ant等,這裡我使用的是maven。

打包成可執行有主類的jar包(jar包中無依賴)

以下是配置打包成可執行,帶主類的jar包:

<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> <!-- 你的主類名 -->            </manifest>          </archive>        </configuration>        ...      </plugin>    </plugins>  </build>

 

But, 雖然現在把程式打包成了jar檔案,也可以運行,但是,這個jar包是沒有包含依賴的,因此,如果這個程式有其他依賴,那麼在運行這個程式的時候,需要指定類目錄,並且要把所有的依賴都放到類目錄下去,手動添加依賴到類目錄下,簡直就是惡夢。。。

我曾經就這麼幹過,它少一個依賴,我就手動加一個到類目錄,少一個,加一個,少一個,加一個,當我加了大概有10幾個之後,而且還看不到盡頭。。。簡直要瘋的節奏,然後想起之前用sbt打包,也是可以把依賴添加到jar包,心情頓時好轉,趕緊google,於是找到google到了maven的官網…。順便說一下,雖然我們google的時候,會搜尋到很多結果,但很多時候,我們最好選擇合適的結果進行閱讀,我的一般順序是:官方優先,StackExchange。。。

打包成帶依賴的jar包配置不加main類而帶依賴的jar包

編輯pom.xml檔案,內容如下:

<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 is used for inheritance merges -->            <phase>package</phase> <!-- bind to the packaging phase -->            <goals>              <goal>single</goal>            </goals>          </execution>        </executions>      </plugin>      [...]</project>

 

加main類也帶依賴的jar包

編輯pom.xml檔案,內容如下:

<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 is 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>

值得注意的是,不要像以下那樣修改pom.xml檔案,那樣雖然不會報錯,但產生的jar包中的MANIFEST.MF中沒有主類,這樣的jar包無法直接運行。

<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> <!-- 你的主類名 -->            </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 is used for inheritance merges -->            <phase>package</phase> <!-- bind to the packaging phase -->            <goals>              <goal>single</goal>            </goals>          </execution>        </executions>      </plugin>      [...]</project>

 

打包

使用以下命令打包:

mvn package

 

就像官方網站說的,” this is pretty simple! “

 


然而,當在運行操作hdfs的jar包時,我出現了以下問題:

 

Exception in thread "main" java.io.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:20)        at sentry.magic.fs.CheckFlumeIsCompleted.<init>(CheckFlumeIsCompleted.java:26)        at Main.main(Main.java:26)

 

好在有位仁兄[2]也遇到了這個問題,從而使得問題解決。解決方案如下:

    FileSystem dfs = 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);    }

 

除了他之種方法之外,我也自己摸索出了一種方法,經過實踐,也是可行的,我的方法如下: 
找到打包好的jar包,然後使用歸檔管理器開啟這個jar包,即“open with archive manager”,找到META-INFO/services目錄,再找到這個目錄下的org.apache.Hadoop.fs.FileSystem檔案,編輯這個檔案,在檔案末尾加上一行:

org.apache.hadoop.hdfs.DistributedFileSystem

 

但是,這麼修改jar的方法,不太合適,因為每產生一次,需要修改一次,所以,建議暫時使用前面那種解決方案。

參考文獻:

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

使用Maven對JAVA程式打包-帶主類、帶依賴【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.