使用Maven构建jar的类加载器

时间:2014-01-03 11:32:25

标签: java eclipse maven

我正在开发一个包含许多子项目的项目。结构类似于Project 1,Project 2和ProjectClassLoader。 使用单独的配置文件,我将项目1和2中的类的二进制名称传递给每次需要作为ProjectClassLoader项目的参数加载。

ProjectClassLoader获取系统类加载器的句柄

ClassLoader loader = ClassLoader.getSystemClassLoader();

理论上允许它加载类路径中包含的任何类。

我正在使用Maven来构建项目并处理它们相关的依赖项。因此,每个项目都有自己独立的pom.xml文件。 ProjectClassLoader定义项目1和2上的父pom.xml,它继承自此。父pom包含项目1和2的依赖项。

我的理解是,这些项目的pom.xml文件中指定的任何依赖项都将在运行时添加到类路径中。但是,当尝试使用系统类加载器加载类时,我发现类没有找到execptions。

我尝试使用mvn:exec插件,我理解在命令行上执行jar时包含类路径中的依赖项,但这没有用。

我非常感谢任何帮助,以进一步理解为什么我可以加载类,即使依赖项是在pom中定义的...提前感谢

1 个答案:

答案 0 :(得分:0)

你能检查一下你的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/xsd/maven-4.0.0.xsd">
    ...
    <properties>
        ...
        <exec.maven.plugin.version>1.2.1</exec.maven.plugin.version>
        ...
    </properties>

    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
            </plugin>
            ...
        </plugins>

        <pluginManagement>
            <plugins>
                ...
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>${exec.maven.plugin.version}</version>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>-classpath</argument>
                            <!-- automatically creates the classpath using all project dependencies, also adding the project build directory -->
                            <classpath />
                            <argument>com.example.Main</argument><!-- your mainClass -->
                        </arguments>
                    </configuration>
                </plugin>
                ...
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        ...
        <dependency>
            <groupId>groupId.for.project1</groupId>
            <artifactId>project1</artifactId>
        </dependency>
        <dependency>
            <groupId>groupId.for.project2</groupId>
            <artifactId>project2</artifactId>
        </dependency>
        ...
    </dependencies>

    <dependencyManagement>
        <dependencies>
            ...
            <dependency>
                <groupId>groupId.for.project1</groupId>
                <artifactId>project1</artifactId>
                <version>${project1.version}</version>
            </dependency>
            <dependency>
                <groupId>groupId.for.project2</groupId>
                <artifactId>project2</artifactId>
                <version>${project2.version}</version>
            </dependency>
            ...
        </dependencies>
    </dependencyManagement>
    ...
</project>

在哪里填写正确的文物。

然后您应该可以使用以下命令启动它:

  

mvn exec:exec

如果它不匹配,你可以发布你的pom plz的配置吗,这样就可以更容易地理解你目前在你的pom中有什么。

相关问题