2013-07-17 50 views
0

我正在配置我的pom.xml来做我想做的事。Maven,maven-assembly-plugin和scala-library.jar文件

我想将我的项目打包为单个可执行jar文件。

下面是我用命令:

  • MVN清洁编译组件:单

我目前的pom.xml做的工作,在目标文件夹中生成的jar文件确实是可执行的,但如果我列出的文件中,有4202个文件(我的helo world +来自scala语言的4201个文件)。

这是正常的吗?是不是有一种方法来建立我的jar与scala-library.jar在里面?怎么样 ?

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.myself</groupId> 
<artifactId>cacrawler</artifactId> 
<version>1.0-SNAPSHOT</version> 
<name>${project.artifactId}</name> 
<description>My wonderfull scala app</description> 
<inceptionYear>2010</inceptionYear> 
<licenses> 
    <license> 
     <name>My License</name> 
     <url>http://....</url> 
     <distribution>repo</distribution> 
    </license> 
</licenses> 

<properties> 
    <maven.compiler.source>1.5</maven.compiler.source> 
    <maven.compiler.target>1.5</maven.compiler.target> 
    <encoding>UTF-8</encoding> 
    <scala.version>2.8.0</scala.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.scala-lang</groupId> 
     <artifactId>scala-library</artifactId> 
     <version>${scala.version}</version> 
    </dependency> 

    <!-- Test --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.scala-tools.testing</groupId> 
     <artifactId>specs_${scala.version}</artifactId> 
     <version>1.6.5</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.scalatest</groupId> 
     <artifactId>scalatest</artifactId> 
     <version>1.2</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<build> 
    <sourceDirectory>src/main/scala</sourceDirectory> 
    <testSourceDirectory>src/test/scala</testSourceDirectory> 
    <plugins> 
     <plugin> 
      <groupId>org.scala-tools</groupId> 
      <artifactId>maven-scala-plugin</artifactId> 
      <version>2.15.0</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> 
         <goal>testCompile</goal> 
        </goals> 
        <configuration> 
         <args> 
          <arg>-make:transitive</arg> 
          <arg>-dependencyfile</arg> 
          <arg>${project.build.directory}/.scala_dependencies</arg> 
         </args> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
     </plugin> 
    <plugin> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <configuration> 
      <archive> 
       <manifest> 
        <mainClass>com.myself.App</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> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.6</version> 
      <configuration> 
       <useFile>false</useFile> 
       <disableXmlReport>true</disableXmlReport> 
       <!-- If you have classpath issue like NoDefClassError,... --> 
       <!-- useManifestOnlyJar>false</useManifestOnlyJar --> 
       <includes> 
        <include>**/*Test.*</include> 
        <include>**/*Suite.*</include> 
       </includes> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
</project> 
+0

你使用哪个maven版本?你使用哪个maven-assembly-plugin版本? – khmarbaise

回答

1

是的,这是正常的。

如果您在jar包中包含scala-library.jar作为jar,那么您应该包含一个自定义代码以加载到您的main(...)中。

java -jar xxx.jar忽略内部瓶瓶

  • 你必须创建<unpack>false</unpack>上dependencySet自己的汇编描述文件(下二进制文件或根,我不记得了)+改变主要从资源加载罐(您main()应注意取决于斯卡拉库)
  • 您可以使用替代组件,专门从事这项工作的(它包括代码发动罐子),喜欢(Maven插件为应存在这些工具):

但我的建议,这是使用Proguard:它会创建一个jar像你当前的代码,但它会收缩无用的代码(很多斯卡拉库的一部分)。通过gui手动尝试(无需首先插入maven-plugin)以找到正确的配置,它可能需要时间(如果使用反射,...)。