简体   繁体   English

具有Maven内置jar的Classloader

[英]Classloader with Maven built jar

I am working on a project which contains a number of subprojects. 我正在一个包含多个子项目的项目中。 The structure is something like Project 1, Project 2 and ProjectClassLoader. 结构类似于Project 1,Project 2和ProjectClassLoader。 Using separate configuration files, I pass in the binary names of the classes from Projects 1 and 2 that need to be loaded each time as arguments to the ProjectClassLoader project. 使用单独的配置文件,我传入了Project 1和Project 2中类的二进制名称,这些类的每次都需要作为ProjectClassLoader项目的参数进行加载。

The ProjectClassLoader gets a handle to the system classloader ProjectClassLoader获取系统类加载器的句柄

ClassLoader loader = ClassLoader.getSystemClassLoader();

which in theory allows it to load any classes which are contained in the classpath. 从理论上讲,这允许它加载类路径中包含的任何类。

I'm using Maven to build the projects and handle their associated dependences. 我正在使用Maven构建项目并处理其相关的依赖关系。 Thus each project has it's own individual pom.xml file. 因此,每个项目都有其自己的单独pom.xml文件。 The ProjectClassLoader defines a parent pom.xml over Projects 1 and 2 which inherit from this. ProjectClassLoader定义了从项目1和项目2继承的父pom.xml。 The parent pom contains dependency entries for both Project 1 and 2. 父pom包含项目1和项目2的依赖项。

My understanding was that any dependencies specified in the pom.xml files of these projects would get added to the classpath at runtime. 我的理解是,这些项目的pom.xml文件中指定的任何依赖项都将在运行时添加到类路径中。 However when trying to load classes using the system classloader, I'm getting class not found execptions. 但是,当尝试使用系统类加载器加载类时,我得到的类未找到执行程序。

I have tried using the mvn:exec plugin which I understand includes the dependencies in the classpath when executing jars on the command line but this has not worked. 我试过使用mvn:exec插件,据我了解,在命令行上执行jar时,该插件在类路径中包含依赖项,但这没有用。

I'd grately appreciate any help in furthering my understanding of why I can load the classes even though the dependencies are defined in the pom...Thanks in advance 我非常感谢您提供任何帮助,以帮助我进一步理解为什么即使在pom中定义了依赖项时也可以加载类的原因。

Can you check if your pom matches this configuration a bit? 您可以检查一下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>

Where you fill them with the correct artifacts. 在其中用正确的工件填充它们。

You should then be able to start it with: 然后,您应该可以使用以下命令启动它:

mvn exec:exec mvn exec:exec

Can you post your configuration for your pom plz if it doesn't match, that way it's easier to understand what exactly you currently have in your pom. 如果您的pom plz配置不匹配,可以将其发布吗,这样可以更轻松地了解您pom中当前所拥有的确切信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM