使用具有maven依赖插件的依赖jar创建库jar

时间:2015-06-11 11:23:14

标签: java maven jar noclassdeffounderror

我想创建一个jar(无主类),后来在apache pig中用作UDF。
当我用maven创建jar时,依赖的jar不包含在输出jar中。

pom文件看起来像这样

<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.test</groupId>
    <artifactId>AppName</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>xzloader-java-pig-udf</name>
    <url>http://maven.apache.org</url>

        <build>
    <plugins>
     <plugin>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.2</version>
       <configuration>
         <source>1.7</source>
         <target>1.7</target>
       </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <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>

    <dependencies>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.8.1</version>
     </dependency>
     <dependency>
        <groupId>org.apache.pig</groupId>
        <artifactId>pig</artifactId>
        <classifier>h2</classifier>
        <version>0.13.0</version>
     </dependency>
    </dependencies>

</project>

但是结果jar总是抛出

  

NoClassDefFoundException

与apache pig一起运行时

1 个答案:

答案 0 :(得分:3)

以下是我的pom.xml的摘录,它构建了一个带依赖关系的jar:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.1</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>                 
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- append to the packaging phase. -->
                <goals>
                    <goal>attached</goal> <!-- goals == mojos -->
                </goals>
            </execution>
        </executions>
    </plugin>

我看到的差异是目标:您使用single,我使用attached