为maven生成一个罐子

时间:2016-01-27 23:29:27

标签: java maven

所以我创建了一个独立的应用程序,它会生成一个jar。当我从Eclipse运行它时,它可以运行,“Run As” - > java应用程序。

当我尝试从控制台运行它时,我开始从swt中获取问题,所以我添加到我的清单swt.jar中,我开始遇到我的下一个依赖于我的pom的问题。

以下是我的swom.jar

的pom.xml示例
Manifest-Version: 1.0
Main-Class: mapper.osm.main.Main
Class-Path: swt.jar

我下载了swt并将其复制到我jar的同一个文件夹中。但我不想手动将所有库添加到清单中。感觉应该有一种更简单,更有效的方式来做到这一点。

这是我添加swt后得到的错误

$ java -jar mapper.jar
java.lang.NoClassDefFoundError: org/opengis/geometry/coordinate/Position
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.opengis.geometry.coordinate.Position
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"

之前完全相同但有一些swt图形库。 这是我的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>mapper</groupId>
    <artifactId>mapper.osm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mapper,osm</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>13.2</geotools.version>
    </properties>

    <dependencies>
        <!-- Eclipse's JFace extensions -->
        <dependency>
            <groupId>org.eclipse.ui</groupId>
            <artifactId>org.eclipse.ui.workbench</artifactId>
            <version>3.7.1.v20120104-1859</version>
        </dependency>
        <!-- SWT -->
        <dependency>
            <groupId>org.eclipse.swt</groupId>
            <artifactId>${swt.artifactId}</artifactId>
            <version>4.4</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>1.12</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- RXTX -->
        <dependency>
            <groupId>org.bidib.jbidib.org.qbang.rxtx</groupId>
            <artifactId>rxtxcomm</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.8</version>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
        </dependency>

        <!-- Instalador izpack -->
        <dependency>
            <groupId>org.codehaus.izpack</groupId>
            <artifactId>izpack-maven-plugin</artifactId>
            <version>5.0.6</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <repositories>
        <repository>
            <id>swt-repo</id>
            <url>https://swt-repo.googlecode.com/svn/repo/</url>
        </repository>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
    </repositories>
    <build>
        <finalName>mapper</finalName>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>mapper.osm.main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mainClass>org.korecky.myjavafx.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>unix-amd64</id>
            <activation>
                <os>
                    <family>unix</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>windows-x86</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>x86</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.win32.win32.x86</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>windows64</id>
            <activation>
                <os>
                    <family>dos</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>windows-x86_64</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>x86_64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>macosx-x86</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>x86</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.cocoa.macosx</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>macosx-x86_64</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>x86_64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.cocoa.macosx.x86_64</swt.artifactId>
            </properties>
        </profile>
    </profiles>
</project>

1 个答案:

答案 0 :(得分:1)

尝试使用Maven Dependency Plugin复制jar,使用“addClasspath”节点复制Maven JAR插件,将依赖项添加到清单中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib</classpathPrefix>
                <mainClass>my.test.App</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

这会将jar复制到target / lib并将它们添加到前缀为lib /

的清单中