【问题标题】:How to import a package within modules (maven)如何在模块中导入包(maven)
【发布时间】:2020-06-15 01:08:29
【问题描述】:

我有一个包含子模块的 mvn 项目。 在模块“moduleC”之一中,包“com.C.cto.wi.cipher.ssl”中有一个类。我需要能够从 moduleA 中的类中使用/调用它。

父 pom:

<groupId>parent_groupId</groupId>
<artifactId>parent_artifactId</artifactId>
<version>parent_version</version>
<packaging>pom</packaging>
<name>parent_artifactId</name>

<modules>
    <module>moduleA</module>
    <module>moduleB</module>
</modules>

moduleB pom:

<parent>
    <groupId>parent_groupId</groupId>
    <artifactId>parent_artifactId</artifactId>
    <version>parent_version</version>
</parent>
<artifactId>moduleB</artifactId>
<packaging>pom</packaging>
<name>moduleB</name>
<modules>
    <module>moduleC</module>
</modules>  

现在,moduleC pom:

<parent>
    <groupId>parent_groupId</groupId>
    <artifactId>moduleB</artifactId>
    <version>parent_version</version>
</parent>
<artifactId>moduleC</artifactId>    
<packaging>bundle</packaging>
<name>moduleC</name>
<dependency>
// List of dependencies
</dependency>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>                
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>SomeName</Bundle-SymbolicName>
                        <Bundle-Name>SomeName</Bundle-Name>
                        <Bundle-Activator>someActivator</Bundle-Activator>
                        <Import-Package>org.osgi.framework</Import-Package>
                        <Export-Package>com.C.cto.wi.cipher.ssl</Export-Package>
                        <Embed-Dependency>
                        </Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
        <finalName>someName</finalName>
    </build>

现在,我在模块 A 中有一个类,它需要访问包“com.C.cto.wi.cipher.ssl”中存在的类。

需要对 pom 进行哪些修改才能实现这一目标?

【问题讨论】:

    标签: java maven pom.xml


    【解决方案1】:

    这取决于你的 pom 文件的结构,但你必须在 moduleA 中包含 moduleC 作为依赖项。

    另外,您已经在 moduleC-info.java 中导出了所需的包:

    exports com.C.cto.wi.cipher.ssl;
    

    最后,确保你修改了 moduleA-info.java:

    requires moduleC;
    

    【讨论】:

    • 我已经编辑了这个问题,详细说明了 pom 的结构。在这种情况下,如何将 moduleC 作为依赖项包含在模块 A 中?你能告诉我代码吗?其次,你指的moduleA-info.java和moduleC-info.java是什么?
    • 我认为您的更新有误。您将模块 B 定义为其自己的父级。另一个问题,您使用 maven-bundle-plugin 有什么具体原因吗?
    • 如果您不熟悉 module-info.java 文件,我建议您从这个简单的教程开始:baeldung.com/java-9-modularityopenjdk.java.net/projects/jigsaw/quick-start。最后,您使用 maven-bundle-plugin 有什么具体原因吗?
    • 我已经修复了更新。我不确定为什么要使用 maven-bundle-plugin。这是一个遗留项目。我不是 100% 知道为什么要使用这个插件。
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2020-04-09
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多