使用maven显示错误的jar构建

时间:2014-10-31 12:33:01

标签: java maven jar pom.xml

我已经构建了一个应用程序,它从某些xml文件的Count标记中获取值,并将其写入输出excel XLSX文件。

我的程序中有2个由main()调用的函数。首先是readXml()读取xml文件,第二个是writeXlsx(),它写入输出excel文件。

我已经使用maven添加依赖项并构建jar文件。

当我运行创建的jar时,它适用于程序的一半,但在尝试编写XLSX时出错。出现错误的是:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
    at com.mpstddn.App.writeXLSX(App.java:107)
    at com.mpstddn.App.main(App.java:64)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook

当我从eclipse运行它时,同样的程序运行良好,但是当我制作一个罐子并运行它时会出错。在我看来jar不能包含apache poi lib。我是maven的新手,不知道我在哪里弄错了。这是我的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mpstddn.java</groupId>
    <artifactId>myMavenProject</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>myMaven</name>
    <url>http://maven.apache.org</url>
    <dependencies>
       <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
        </exclusions>
      </dependency>
      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>openxml4j</artifactId>
          <version>1.0-beta</version>
      </dependency>     
       <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.10-FINAL</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
     </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <scriptSourceDirectory>src\main\scripts</scriptSourceDirectory>
        <testSourceDirectory>src\test\java</testSourceDirectory>
        <outputDirectory>target\classes</outputDirectory>
        <testOutputDirectory>target\test-classes</testOutputDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>lib</directory>
            <excludes>
                <exclude>**/*.java</exclude>
                <exclude>**/*.jar</exclude>
            </excludes>
        </resource>
 </resources>
 <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.0</version>
            </plugin>
        </plugins>
 </pluginManagement>
  <plugins>
        <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                        <archive>
                                <manifest>
                                        <mainClass>com.mpstddn.App</mainClass>
                                </manifest>
                        </archive>
                </configuration>
            </plugin>
       <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <executions>
                <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <id>default-clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>default-install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-testResources</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testResources</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <executions>
                  <execution>
                    <id>default-test</id>
                      <phase>test</phase>
                     <goals>
                        <goal>test</goal>
                     </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
             <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                     <manifest>
                         <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                             <mainClass>com.mpstddn.App</mainClass>
                      </manifest>
                 </archive>
            </configuration>
             <executions>
                <execution>
                     <id>default-jar</id>
                    <phase>package</phase>
                    <configuration>
                        <packagingExcludes>*.jar</packagingExcludes>
                    </configuration>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
          <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <executions>
                <execution>
                    <id>default-site</id>
                    <phase>site</phase>
                    <goals>
                        <goal>site</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target\site</outputDirectory>
                        <reportPlugins>
                            <reportPlugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </reportPlugin>
                        </reportPlugins>
                    </configuration>
                </execution>
                <execution>
                    <id>default-deploy</id>
                    <phase>site-deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target\site</outputDirectory>
                        <reportPlugins>
                            <reportPlugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </reportPlugin>
                        </reportPlugins>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <outputDirectory>target\site</outputDirectory>
                <reportPlugins>
                    <reportPlugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                    </reportPlugin>
                </reportPlugins>
            </configuration>
        </plugin>
       </plugins>
     </build>
 </project>

我创建jar的方式: right click on project in eclipse -> run as -> Maven install 这会在项目文件夹中的target目录中创建一个jar文件。

我通过输入以下命令提示符运行jar文件:

java -jar myMavenProject-0.0.1-SNAPSHOT.jar X:\input_directory input.xml 9

1 个答案:

答案 0 :(得分:1)

所以你的问题不是基于依赖,而是因为你配置了maven-assembly-plugin错误:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
           <id>package</id>
           <goals>
             <goal>single</goal>
           <goals>
           <phase>package</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
 </build>
</project>

问题是maven-assembly-plugin默认不绑定到任何生命周期阶段。然后尝试:

mvn clean package

并检查生成的jar文件(target/WhatEver-x.x-SNAPSHOT-jar-with-dependencies.jar)。