Maven 仅复制特定的依赖项

Maven copy only specific dependencies

我正在按照基于组件的软件工程使用 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/ 的文件夹中?

感谢您的帮助。

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

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

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>