Idea使用Maven編譯scala和打包jar
阿新 • • 發佈:2018-12-22
下面Maven的pom檔案
- <properties>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- <encoding>UTF-8</encoding>
- <scala.version>2.11.8</scala.version>
- <scala.compat.version
>2.11</scala.compat.version> - </properties>
- <dependencies>
- <dependency>
- <groupId>org.scala-lang</groupId>
- <artifactId>scala-library</artifactId>
- <version>${scala.version}</version>
- </dependency
> - </dependencies>
- <build>
- <!--scala待編譯的檔案目錄-->
- <sourceDirectory>src/main/scala</sourceDirectory>
- <testSourceDirectory>src/test/scala</testSourceDirectory>
- <!--scala外掛-->
- <plugins>
- <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>
- <configuration>
- <args>
- <!--<arg>-make:transitive</arg>--><!--scala2.11 netbean不支援這個引數-->
- <arg>-dependencyfile</arg>
- <arg>${project.build.directory}/.scala_dependencies</arg>
- </args>
- </configuration>
- </execution>
- </executions>
- </plugin>
- <!--manven打包外掛-->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>2.4.3</version>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <filters>
- <filter>
- <artifact>*:*</artifact>
- <excludes>
- <exclude>META-INF/*.SF</exclude>
- <exclude>META-INF/*.DSA</exclude>
- <exclude>META-INF/*.RSA</exclude>
- </excludes>
- </filter>
- </filters>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>reference.conf</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>cn.itcast.rpc.Master</mainClass> <!--main方法-->
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
上面是使用Scala2.11的版本,注意scala2.11 netbean不支援這個引數-make:transitive這個引數,所以必須得註釋掉
下面是簡單的測試:
使用Maven進行jar依賴打包,之前使用Idea的artifacts進行打包,但是匯出的jar有可能因為缺少某些資訊而無法直接執行,但是使用maven自動的pakage便沒有這個問題,而且jar包的大小明顯小於artifacts的jar包大小,下面是使用方法
下載完相關的依賴後,會在target的目錄下生成下專案的jar包