Maven&Intellij IDEA打jar包以及創(chuàng)建scala project

1.Maven

1.1 Maven依賴:

進(jìn)入這個網(wǎng)址查jar包依賴
http://mvnrepository.com/
將xml代碼復(fù)制到你的pom.xml中即可。

如果有時候用的是別人的代碼。
可以打開pom.xml文件,按“alt + insert”,
彈出:



點(diǎn)擊dependency



搜索缺少的包,點(diǎn)擊“add”。
idea中在這里配置自己的maven(maven,設(shè)置文件,本地倉庫),有時候自己配置的新版本的maven會有不兼容的問題,也可以用idea自帶的。

1.2 Maven 常用指令:

常用命令為 :

1.mvn archetype:create :創(chuàng)建 Maven 項目
2.mvn compile :編譯源代碼
3.mvn clean compile:清理并編譯
4.mvn test-compile :編譯測試代碼
5.mvn test : 運(yùn)行應(yīng)用程序中的單元測試
6.mvn site : 生成項目相關(guān)信息的網(wǎng)站
7.mvn clean :清除目標(biāo)目錄中的生成結(jié)果
8.mvn package : 依據(jù)項目生成 jar 文件
9.mvn deploy:生成jar文件并上傳到本地和私服倉庫
10.mvn install    在本地repository中安裝jar(包含mvn compile,mvn package,然后上傳到本地倉庫)

有時候需要把自己的包上傳到本地maven作為依賴包。

如果需要在里面加入外部的jar包,需要在maven中安裝。
進(jìn)入maven的bin文件夾,如果配置了環(huán)境變量不在bin文件夾也可以,用指令安裝該jar包,Dfile參數(shù)可以寫入文件的路徑,但是我試了一下失敗了,cd到該文件目錄下,不寫路徑安裝就成功了。
在IDEA終端中輸入:
mvn install:install-file -Dfile=xgboost4j-spark-0.7.jar -DgroupId=ml.dmlc -DartifactId=xgboost4j -Dversion=0.7 -Dpackaging=jar
mvn install:install-file -Dfile=scopt_2.10-3.3.0.jar -DgroupId=com.github.scopt -DartifactId=scopt_2.10 -Dversion=3.3.0 -Dpackaging=jar
再添加依賴就正常了
<dependency>
<groupId>com.github.scopt</groupId>
<artifactId>scopt_2.10</artifactId>
<version>3.3.0</version>
</dependency>

-Dfile:包的本地真實地址(如果在當(dāng)前文件夾下,直接寫名字)
-DgroupId:pom.xml中g(shù)roupId
-DartifactId:pom.xml中artifactId
-Dversion:pom.xml中版本號
-Dpackaging:jar或war,包的后綴名
-Dclassifier:兄弟包的別名,也就是-Dversion值后面的字符workspace-1.1.1-SNAPSHOT-core.jar的-core,我沒有用到。

第一種方法:

在本地 Repository 中安裝 jar。
Dfile參數(shù)是該jar包的全路徑,-DartifactId、-Dversion等屬性要與pom.xml文件中的依賴屬性一致:
Dfile路徑最好是直接在D盤C盤這種文件夾下面,試過把文件放在桌面上,發(fā)現(xiàn)識別不了
-Dversion= pom.xml中的版本號
-DartifactId=pom.xml中的artifactId
-DgroupId=pom.xml中的groupId

mvn install
(mvn install:install-file -Dfile=新框架.jar -DgroupId=com. xx.xx -DartifactId=NAME -Dversion=0.0.1 -Dpackaging=jar  -DgeneratePom=true)

mvn install:install-file -Dfile=xgboost4j-0.7.jar -DgroupId=ml.dmlc -DartifactId=xgboost4j -Dversion=0.7 -Dpackaging=jar

如果顯示build success,則安裝成功了,可以去maven的本地目錄查看一下文件是否生成。

第二種方法

讀取系統(tǒng)本地的jar包,改依賴中的參數(shù)即可。

    <dependency>    
      <groupId>org.jblas</groupId>     
        <artifactId>jblas</artifactId>   
          <version>1.2.3</version>     
      <scope>system</scope>
    <systemPath>${basedir}/*****.jar</systemPath>
    </dependency>

install&package&deploy的區(qū)別
package命令完成了項目編譯、單元測試、打包功能。用于生成jar包。
install完成package的功能以后,同時把打好的可執(zhí)行jar包布署到本地maven倉庫,這樣可以通過修改maven依賴來使用。
deploy完成install功能以后,同時把打好的可執(zhí)行jar包布署到本地maven倉庫和遠(yuǎn)程maven私服倉庫。

2.用Intellij IDEA 打包jar

如果之前已經(jīng)打過jar包的話,先刪除META-INF文件夾下的MANIFEST.MF配置文件,才能重新生成jar包。

2.1.File->Project Structure->Artifacts->Add->Jar->From modules with dependencies

ctrl + alt + shift +s或者點(diǎn)擊圖標(biāo)



來打開Project Structure。

然后,點(diǎn)擊add光標(biāo)

來添加jar包。

2.2.配置

第一步選擇Main函數(shù)執(zhí)行的類。



第二步選擇如圖的選項,目的是對第三方Jar包打包時做額外的配置,如果不做額外的配置可不選這個選項(但不保證打包成功)



第三步刪除不需要的模塊
按住shift點(diǎn)擊不需要的模塊,然后點(diǎn)擊remove刪除。

通常是留下最后的文件夾,其他全部刪除。



第三步點(diǎn)擊build,然后選擇build artifacts,就能在out文件夾里找到j(luò)ar包。

2.3.直接打包

使用IDEA + Maven 創(chuàng)建scala項目時,勾選Create for archetype,找到org.scala-tools.archetypes:scala-archetype-simple然后點(diǎn)next。
創(chuàng)建完項目以后出現(xiàn):Maven projects need to be imported
選擇Enable Auto-Import,這樣就會自動配置pom.xml文件
右邊會出現(xiàn)Maven Projects,點(diǎn)開里面會有運(yùn)行編譯測試等等功能。

點(diǎn)擊build(ctrl + F9)


windows build快捷鍵

mac build快捷鍵

再點(diǎn)擊package



jar包就會出現(xiàn)在target文件夾下。

注意事項:

此方法需要刪除test文件夾,否則打包的時候會報錯。

如果不打算刪除test文件夾,那么需要再對應(yīng)pom.xml文件目錄中構(gòu)建這兩個文件夾路徑。(src/main/scala和src/test/scala)

    <build>
      <sourceDirectory>src/main/scala</sourceDirectory>
      <testSourceDirectory>src/test/scala</testSourceDirectory>
    </build>
另外<properties>中可能指定了scala版本之類的信息:
    <properties>
        <spark.version>2.2.1</spark.version>
        <scala.version>2.11.8</scala.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

如果package時打包出錯,并出現(xiàn)scala版本的問題可以把<properties>這一項改成:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
如果Maven Project 這一欄隱藏了,點(diǎn)擊搜索圖標(biāo)

搜索 Maven Project,再點(diǎn)擊即可。

3.IDEA加載github下載的java,scala項目

代碼通過git clone或者下載zip得到

3.1 打開

選擇 File-->Open... 打開下載的項目

3.2 配置JDK

File--> Project Structure ---> Project---->選擇JDK (不能選就new一個選中本機(jī)JDK)

3.3 選擇 Modules

Project Structure界面,設(shè)置目錄。
target或out設(shè)置Excluded,src設(shè)置Sources


3.4 程序包junit.framework不存在

編譯時會報錯,報“junit.framework不存在”錯誤。
一般來說pom中有junit包的依賴,但是有scope屬性。

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>

依賴加入scope屬性后,就會被約束在該生命周期內(nèi),在compile階段不引入test階段的依賴,也就是說編譯階段該依賴無效??梢詣h除scope,或者修改scope屬性:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>compile</scope>
</dependency>

4.創(chuàng)建scala project

4.1.新建project,并勾選
4.2.按兩下shift搜索scala-archetype-simple


4.3.修改pom依賴

通常我這邊依賴寫成:

<dependencies>
  <dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.11.0</version>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.11</artifactId>
    <version>2.2.1</version>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-mllib_2.11</artifactId>
    <version>2.2.1</version>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.11</artifactId>
    <version>2.1.1</version>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-hive_2.11</artifactId>
    <version>2.1.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-streaming_2.11</artifactId>
    <version>2.2.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.4.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-yarn-client</artifactId>
    <version>2.4.1</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.4</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.specs</groupId>
    <artifactId>specs</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
  </dependency>
</dependencies>

有時候會報錯
expected START_TAG or END_TAG not TEXT
這是依賴前面有空格造成的,刪除空格以后直接回車。


有時候
為了避免沖突,需要用<exclusions>剔除部分jar包。
例如已經(jīng)引入了hadoop-client依賴,而spark-core中含有hadoop-client包,則可以使用<exclusions>剔除

   <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>${hadoop.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.11</artifactId>
      <version>${spark.version}</version>
      <scope>provided</scope>
      <exclusions>
        <exclusion>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-client</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

其中Spark-Core提供Spark最基礎(chǔ)與最核心的功能,主要包括以下功能:
(1)SparkContext:
通常而言,Driver Application的執(zhí)行與輸出都是通過SparkContext來完成的。在正式提交Application之前,首先需要初始化SparkContext。SparkContext隱藏了網(wǎng)絡(luò)通信、分布式部署、消息通信、存儲能力、計算能力、緩存、測量系統(tǒng)、文件服務(wù)、Web服務(wù)等內(nèi)容,應(yīng)用程序開發(fā)者只需要使用SparkContext提供的API完成功能開發(fā)。SparkContext內(nèi)置的DAGScheduler負(fù)責(zé)創(chuàng)建Job,將DAG中的RDD劃分到不同的Stage,提交Stage等功能。內(nèi)置的TaskScheduler負(fù)責(zé)資源的申請,任務(wù)的提交及請求集群對任務(wù)的調(diào)度等工作。
(2)存儲體系:
Spark優(yōu)先考慮使用各節(jié)點(diǎn)的內(nèi)存作為存儲,當(dāng)內(nèi)存不足時才會考慮使用磁盤,這極大地減少了磁盤IO,提升了任務(wù)執(zhí)行的效率,使得Spark適用于實時計算、流式計算等場景。此外,Spark還提供了以內(nèi)存為中心的高容錯的分布式文件系統(tǒng)Tachyon供用戶進(jìn)行選擇。Tachyon能夠為Spark提供可靠的內(nèi)存級的文件共享服務(wù)。
(3)計算引擎:
計算引擎由SparkContext中的DAGScheduler、RDD以及具體節(jié)點(diǎn)上的Executor負(fù)責(zé)執(zhí)行的Map和Reduce任務(wù)組成。DAGScheduler和RDD雖然位于SparkContext內(nèi)部,但是在任務(wù)正式提交與執(zhí)行之前會將Job中的RDD組織成有向無環(huán)圖(DAG),并對Stage進(jìn)行劃分,決定了任務(wù)執(zhí)行階段任務(wù)的數(shù)量、迭代計算、shuffle等過程。
(4)部署模式:
由于單節(jié)點(diǎn)不足以提供足夠的存儲和計算能力,所以作為大數(shù)據(jù)處理的Spark在SparkContext的TaskScheduler組件中提供了對Standalone部署模式的實現(xiàn)和Yarn、Mesos等分布式資源管理系統(tǒng)的支持。通過使用Standalone、Yarn、Mesos等部署模式為Task分配計算資源,提高任務(wù)的并發(fā)執(zhí)行效率。

4.4.插件(maven-shade-plugin,maven-jar-plugin)

有的時候需要引用一些外部包或者文件,需要把集群中沒有的庫或者文件一起打進(jìn)jar包。例如可以用shade把分詞工具ansj打進(jìn)jar包:
maven-jar-plugin
使用maven-jar-plugin這個插件可以去掉一些不想要的文件或者類。

           <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-jar-plugin</artifactId>
                 <version>3.1.0</version>
                 <configuration>
                     <archive>
                         <manifest>
                             <!-- 指定入口函數(shù) -->
                             <mainClass>com.main.Jar</mainClass>
                             <!-- 是否添加依賴的jar路徑配置 -->
                            <addClasspath>true</addClasspath>
                             <!-- 依賴的jar包存放未知,和生成的jar放在同一級目錄下 -->
                             <classpathPrefix>lib/</classpathPrefix>
                         </manifest>
                     </archive>
                     <!-- 不打包c(diǎn)om.library下面的所有文件或類 -->
                     <excludes>com.library/*</excludes>
                 </configuration>
            </plugin>

maven-shade-plugin
依賴:
為了防止打出來的jar包占用太大空間,已有的包不需要使用shade打包,集群中沒有的才需要。在已經(jīng)有的包后面加上<scope>provided</scope>,這樣這些庫就不會被打包,沒有這句的包會打進(jìn)jar包。

<dependencies>
  <dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.11.0</version>
  </dependency>

  <dependency>
      <groupId>org.ansj</groupId>
      <artifactId>ansj_seg</artifactId>
      <version>5.1.1</version>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.11</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-mllib_2.11</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.11</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-hive_2.11</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
<version>2.4.1</version>
<scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-yarn-client</artifactId>
<version>2.4.1</version>
<scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.4</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.specs</groupId>
    <artifactId>specs</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
  </dependency>
</dependencies>

  <build>
//插件
    <plugins>
      <!-- the Maven compiler plugin will compile Java source files 編譯JAVA的插件-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <!-- the Maven Scala plugin will compile Scala source files 編譯scala插件-->
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- configure the eclipse plugin to generate eclipse project descriptors for a Scala project -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.10</version>
        <configuration>
          <projectnatures>
            <projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
          </projectnatures>
          <buildcommands>
            <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
          </buildcommands>
          <classpathContainers>
            <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer>
            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
          </classpathContainers>
          <excludes>
            <exclude>org.scala-lang:scala-library</exclude>
            <exclude>org.scala-lang:scala-compiler</exclude>
          </excludes>
          <sourceIncludes>
            <sourceInclude>**/*.scala</sourceInclude>
            <sourceInclude>**/*.java</sourceInclude>
          </sourceIncludes>
        </configuration>
      </plugin>

      <!-- allows the route to be run via 'mvn exec:java' -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <mainClass>UP.MyRouteMain</mainClass>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <shadedArtifactAttached>true</shadedArtifactAttached>
//有時候需要同時部署使用shade和不使用shade兩種jar包
//那么在shadedClassifierName后加上名稱,該名稱作為后綴在shade構(gòu)件jar包后
              <shadedClassifierName>wangyao</shadedClassifierName>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

4.5. 更新插件
Intellj Idea 可以自動載入Maven依賴,但有使用Mac book的時候常常會碰到問題,導(dǎo)致pom文件修改卻沒有觸發(fā)自動重新載入的動作。點(diǎn)擊Maven Project=》你的項目名稱=》Plugins,一些插件下面有紅色下劃線且尾部帶有"unknow",此時就需要手動強(qiáng)制更新依賴。
兩個方法:
(1)右鍵單擊項目;
(2)點(diǎn)擊Maven=》Reimport菜單項。


或者,IDEA將通過網(wǎng)絡(luò)自動下載相關(guān)依賴,并存放在Maven的本地倉庫中。另外,可以將Maven的刷新設(shè)置為自動,配置方法為:

(1)單擊File|Setting菜單項,打開Settings選項卡;
(2)在左側(cè)的目錄樹中,展開Maven節(jié)點(diǎn);
(3)勾選Import Maven projects automatically選擇項。
(4) 項目右鍵-》Maven-》Reimport
(5) 操作之后你就可以發(fā)現(xiàn)maven的依賴包已經(jīng)更新!

4.6 Maven鏡像

在maven里創(chuàng)建MyRepository目錄,
我一般叫maven-dependcies,那就是<localRepository>D:/maven-dependcies</localRepository>
打開maven下的conf\settings.xml,如果是idea自帶的要去idea的插件里面找maven,在conf文件夾里。
在<settings>后面加上
<localRepository>maven路徑/MyRepository</localRepository>
對應(yīng)自己MyRepository的路徑。

國外源下載太慢,在setting.xml中mirrors節(jié)點(diǎn)中增加一段代碼:

 <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
 </mirror> 

使用阿里云的鏡像速度會更快。

5.新建package



6.在package里新建scala object


7.package顯示
有的時候package是疊在一起的,沒法再一個package下創(chuàng)建多個子package。
點(diǎn)擊小齒輪,然后把Hide Empty Middle Packages取消就可以了


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容