使用maven运行java主程序

时间:2014-03-06 09:31:51

标签: java maven

使用以下命令运行执行maven主类时:

mvn exec:java -Dexec.mainClass="com.xoxo.amqtest.SubscriberDriver"

我认为必须从maven回购中挑选所有的罐子。但它会抛出异常。

java.lang.NoClassDefFoundError: com/xoxo/infra/protectedpkg/ProtectedPackageLoadException

这个maven项目在日食中运行良好。有没有办法指定maven从maven repo中选择所有依赖项,而不是添加所有依赖项,如下所示

java -cp ./:./target/amq-subscriber-1.0.0-SNAPSHOT-jar-with-dependencies.jar:/x/home/stvu/.m2/repository/com/xoxo/submodule/infra-jsse-2.0.1.jar com.xoxo.amqtest.SubscriberDriver

编辑:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>com.xoxox.infra</groupId>
        <artifactId>infra-parent</artifactId>
        <version>13.2.3</version>
        <relativePath />
    </parent>

    <groupId>com.xoxox.amqtest</groupId>
    <artifactId>amqsubscriber</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>amq_sub_test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xoxox.infra</groupId>
            <artifactId>infra</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xoxox.kernel</groupId>
            <artifactId>KernelDAL</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.11</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit-addons</groupId>
            <artifactId>junit-addons</artifactId>
            <version>1.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</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> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            com.xoxox.infra.maven.plugins
                                        </groupId>
                                        <artifactId>
                                            infra-codegenerator-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [13.3.0,)
                                        </versionRange>
                                        <goals>
                                            <goal>generate-code</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2 个答案:

答案 0 :(得分:2)

我过去使用exec-maven-plugin

完成了这项工作
           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>execute-your-main</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.xoxo.amqtest.SubscriberDriver</mainClass>
                            <arguments>
                                <argument>ADD_YOUR_ARGUMENTS_IF_NEEDED</argument>
                            </arguments>
                        </configuration>
                    </execution>
            </plugin>

答案 1 :(得分:0)

看看appassembler插件。

http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/

配置看起来像

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <programs>
                    <program>
                        <mainClass>fun.Tester</mainClass>
                        <id>app</id>
                    </program>
                </programs>
            </configuration>
        </plugin>

它会在你的目标目录中创建目录名'appassembler',它将根据'bin'目录中的平台拥有所有依赖的jar和可执行文件