1. 程式人生 > >Idea使用Maven編譯scala和打包jar

Idea使用Maven編譯scala和打包jar

下面Maven的pom檔案

  1. <properties>
  2. <maven.compiler.source>1.8</maven.compiler.source>
  3. <maven.compiler.target>1.8</maven.compiler.target>
  4. <encoding>UTF-8</encoding>
  5. <scala.version>2.11.8</scala.version>
  6. <scala.compat.version
    >
    2.11</scala.compat.version>
  7. </properties>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.scala-lang</groupId>
  11. <artifactId>scala-library</artifactId>
  12. <version>${scala.version}</version>
  13. </dependency
    >
  14. </dependencies>
  15. <build>
  16. <!--scala待編譯的檔案目錄-->
  17. <sourceDirectory>src/main/scala</sourceDirectory>
  18. <testSourceDirectory>src/test/scala</testSourceDirectory>
  19. <!--scala外掛-->
  20. <plugins>
  21. <plugin>
  22. <groupId
    >
    net.alchim31.maven</groupId>
  23. <artifactId>scala-maven-plugin</artifactId>
  24. <version>3.2.2</version>
  25. <executions>
  26. <execution>
  27. <goals>
  28. <goal>compile</goal>
  29. <goal>testCompile</goal>
  30. </goals>
  31. <configuration>
  32. <args>
  33. <!--<arg>-make:transitive</arg>--><!--scala2.11 netbean不支援這個引數-->
  34. <arg>-dependencyfile</arg>
  35. <arg>${project.build.directory}/.scala_dependencies</arg>
  36. </args>
  37. </configuration>
  38. </execution>
  39. </executions>
  40. </plugin>
  41. <!--manven打包外掛-->
  42. <plugin>
  43. <groupId>org.apache.maven.plugins</groupId>
  44. <artifactId>maven-shade-plugin</artifactId>
  45. <version>2.4.3</version>
  46. <executions>
  47. <execution>
  48. <phase>package</phase>
  49. <goals>
  50. <goal>shade</goal>
  51. </goals>
  52. <configuration>
  53. <filters>
  54. <filter>
  55. <artifact>*:*</artifact>
  56. <excludes>
  57. <exclude>META-INF/*.SF</exclude>
  58. <exclude>META-INF/*.DSA</exclude>
  59. <exclude>META-INF/*.RSA</exclude>
  60. </excludes>
  61. </filter>
  62. </filters>
  63. <transformers>
  64. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  65. <resource>reference.conf</resource>
  66. </transformer>
  67. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  68. <mainClass>cn.itcast.rpc.Master</mainClass> <!--main方法-->
  69. </transformer>
  70. </transformers>
  71. </configuration>
  72. </execution>
  73. </executions>
  74. </plugin>
  75. </plugins>
  76. </build>

上面是使用Scala2.11的版本,注意scala2.11 netbean不支援這個引數-make:transitive這個引數,所以必須得註釋掉

下面是簡單的測試:


使用Maven進行jar依賴打包,之前使用Idea的artifacts進行打包,但是匯出的jar有可能因為缺少某些資訊而無法直接執行,但是使用maven自動的pakage便沒有這個問題,而且jar包的大小明顯小於artifacts的jar包大小,下面是使用方法


下載完相關的依賴後,會在target的目錄下生成下專案的jar包