【问题标题】:Maven package with source and attached dependecies带有源和附加依赖项的 Maven 包
【发布时间】:2013-03-27 13:55:48
【问题描述】:

我很难使用 maven 将 java 源文件和附加的依赖项添加到输出 jar 文件中。

我想要归档的是在一个可以分发的 jar 中包含已编译的类、源文件和所有依赖项(无源)。

- com/name/project/*.class
- com/name/project/*.java
- lib/*.jar
- META-INF/MANIFEST.MF  

这是我目前所拥有的 pom.xml:

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.name.project/main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

希望有人能帮助我。

干杯 丹

【问题讨论】:

  • 简而言之。错误的方法。使用支持已有源代码、javadoc 包等的常用 Maven 方式。

标签: java maven


【解决方案1】:

我决定不将dependecy-jars 添加到我的输出jar 中。相反,我使用以下 pom.xml 将我的源代码与已编译的依赖项一起包含到 jar 中。 我不得不排除 */.java 文件,因为一些奇怪的依赖关系也包含了源代码。

<build>
    <sourceDirectory>src/</sourceDirectory>
    <resources>
        <resource>
            <directory>${project.build.sourceDirectory}</directory>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <excludes>**\/*.java</excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

感谢您的帮助。

干杯 丹

【讨论】:

    【解决方案2】:

    一个 JAR 通常真的不应该包含其他 JAR,但当然没有什么可以阻止你这样做。

    您可以使用resource 部分整齐地包含源代码,如下所示:

    <build>
        </resources>
            <resource>
                <directory>src/main/java</directory>
            </resource>
        </resources>
        ...
    

    我大胆猜测您可能能够将您的copy-dependencies 任务绑定到resources 阶段,看看这是否会在最终工件中包含依赖项 JAR。

    【讨论】:

    • 是的,目前依赖只是复制到target/lib/目录下,不包含在jar文件中。
    • 我已经更新了答案 - 尝试将 copy-dependencies 绑定到 resources 阶段。
    【解决方案3】:

    也许你应该看到this link。只需检查您是否正确配置了您的 maven+IDE 合作

    【讨论】:

      猜你喜欢
      • 2015-08-07
      • 2010-12-20
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      相关资源
      最近更新 更多