Maven 手動添加第三方依賴包及編譯打包和java命令列編譯JAVA檔案並使用jar命令打包

來源:互聯網
上載者:User

標籤:

一,執行個體:建立了一個Maven項目,在eclipse中通過 build path –> configure path….將依賴包添加到工程中後,eclipse不報錯了。但是用Maven命令 mvn clean compile 時出錯如下:

原因是在eclipse中添加了 exteneral jar後,還需要在maven中再添加依賴的jar包

 

Maven手動添加第三方依賴包有兩種方法:

參考:maven 添加第三方依賴              maven中把依賴的JAR包一起打包

第一種方法是使用Maven命令添加,另一種方法則是修改pom.xml檔案添加。

現介紹修改pom.xml檔案添加:假設我的依賴包在C:\Users\xxx\Desktop\storm.starter\storm.starter\lib\datasource.jar

在pom.xml中添加dependency如下:

<dependency>            <groupId>com.my.test</groupId>            <artifactId>datasource</artifactId>            <version>1.0</version>            <scope>system</scope>            <systemPath>C:\Users\xxx\Desktop\storm.starter\storm.starter\lib\datasource.jar</systemPath>        </dependency>

再重新編譯即可成功,會在項目的根目錄下產生 target\classes 目錄,裡面就是產生的各種 .class 檔案

 

再運行, mvn clean package 就可以將所有的編譯好的 .class 檔案打成 jar包了。但是,這裡貌似沒有解決代碼中存在依賴第三方jar包的問題。???

可以使用maven-assembly-plugin外掛程式將工程依賴的jar都一起打包,在pom.xml中添加配置如下:

<build>    <plugins>      <plugin>        <artifactId>maven-assembly-plugin</artifactId>        <version>2.4</version>        <configuration>          <descriptorRefs>            <descriptorRef>jar-with-dependencies</descriptorRef>          </descriptorRefs>        </configuration>        <executions>          <execution>            <id>make-assembly</id>            <phase>package</phase>            <goals>              <goal>single</goal>            </goals>          </execution>        </executions>      </plugin>    </plugins>  </build>

這時,Maven將會自動地根據 <dependency> </dependency>中的 <scope> </scope>參數來決定將哪些依賴的jar包也一起打包。

 

另外,用Maven編譯運行項目的一些知識參考:Maven編譯代碼的相關命令

 

二,在命令列中編譯JAVA檔案。假設 Test.java 存放在 com\my\test\Test.java ,且 Test.java 原始碼裡面的package語句: package com.my.test;

編譯 :         javac  com\my\test\Test.java

打jar包:     jar cvf  test.jar  com\test\my\Test.class

 

查看jar包中的內容:   jar vtf  test.jar

Maven 手動添加第三方依賴包及編譯打包和java命令列編譯JAVA檔案並使用jar命令打包

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.