Maven仅复制特定的依赖项

时间:2020-04-22 22:18:13

标签: java maven components osgi dependency-management

我正在使用基于组件的软件工程,使用Maven开发OSGI电子邮件客户端。我必须确保在OSGI容器内解析了我所有组件之间的依赖关系,因此我无法在生成的JAR内复制依赖关系,否则使用OSGI将毫无意义。但是我确实必须在JAR内部复制一个依赖项,即javax.mail,因为我找不到任何与OSGI兼容的捆绑包,可以通过电子邮件发送邮件。

为此,我看过以下页面:https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html

所以我编辑了pom.xml

<project>
    ...
    <build>
        <plugins>
            <plugin> <!-- to edit the MANIFEST.MF, required for OSGI -->
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>4.2.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Class-Path>lib/</Class-Path>
                        ... OSGI instructions ...
                    </instructions>
                </configuration>
            </plugin>
            <plugin> <!-- to copy the dependencies -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.sun.mail</groupId>
                                    <artifactId>javax.mail</artifactId>
                                    <version>1.6.2</version>
                                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
</project>

但是<artifactItems>标签似乎不起作用。当我mvn install时,它将所有依赖项复制到dependency/文件夹而不是lib/文件夹中。如何仅将javax.mail JAR复制到名为lib/的文件夹中?

谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您混合了目标copy-dependenciescopy。将copy-dependencies替换为copy

http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html

答案 1 :(得分:1)

maven-bundle-plugin允许嵌入依赖项: https://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

<Embed-Dependency>javax.mail|javax.mail-api</Embed-Dependency>