【问题标题】:How to create source distribution with self sustainable maven build?如何使用自我可持续的 Maven 构建创建源代码分发?
【发布时间】:2010-11-14 12:50:33
【问题描述】:

我想要做的是创建我的应用程序的所有依赖项的源代码分发并将其刻录到 DVD 上。 这样我就可以在 100 年内建造它(好吧,好吧,你知道我的意思......)。没有对库或 maven 插件的在线依赖!

我知道 Ant 会更好,但我在我的项目中使用 maven。我不会仅仅为了这个而切换到 Ant,我在问如何用 maven 做到这一点。或者,如果有一种方法可以生成自我可持续的 Ant 构建,我可以把它放在 DVD 上,那也很棒。 (有ant:ant 插件,但它只生成Ant build.xml,将依赖项指向本地maven repo)

我采用的方法是创建可以放在 DVD 上的特殊本地存储库,然后使用 mvn -o -Dmaven.repo.local=repo/on/dvd 构建项目。我试图通过将dependency:copy-dependencies 和useRepositoryLayout 参数设置为true 来创建这样的存储库。但它不会复制我的构建所依赖的该死的 maven 插件......

【问题讨论】:

    标签: maven-2 build-process build


    【解决方案1】:

    观看: Maven Assembly Plugin

    引用自主页:

    你想创建一个二进制文件 来自 Maven 项目的分发 包括支持脚本, 配置文件和所有运行时 依赖?您需要使用 装配插件创建一个 为您的项目分发。

    它的可配置性很好。我特别用它来制作带有嵌入式码头服务器和用户文档的网络应用程序的自运行演示版本。

    【讨论】:

      【解决方案2】:

      我没有完整的答案。上次我看这个时,我认为在构建开始时(或使用单独的)清理 localRepository 和正在运行的mvn dependency:go-offline。

      如果您真的很热衷,您还希望将 maven 本身和 JDK 捆绑到发行版中。这可能会使其超出纯 maven 构建的范围。

      【讨论】:

        【解决方案3】:

        我能想到的包含插件的唯一方法是在命令行上为构建指定不同的本地存储库并确保下载所有依赖源等,然后创建一个包含项目内容和自定义存储库的存档.

        这是一个下载源代码和 javadocs 的 pom(它将它们下载到项目的目标目录,我们将其从存档中排除,因为它们也将在本地存储库中)。程序集描述符将项目的内容和本地存储库捆绑到一个(相当大的)存档中。

        请注意,所有处理都在配置文件中,因为您真的不希望在每次构建时都运行它。如果临时本地存储库位于目标目录中,您可以在之后使用 mvn clean 轻松清理混乱。 要激活配置文件,请执行以下操作:

        mvn package -Parchive -Dmaven.repo.local=.\target\repo
        

        这是 pom:

        <?xml version="1.0" encoding="UTF-8"?>
        <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/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>name.seller.rich</groupId>
          <artifactId>test-archive</artifactId>
          <version>0.0.1</version>
          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.5</version>
              <scope>test</scope>
            </dependency>
          </dependencies>
          <profiles>
            <profile>
              <id>archive</id>
              <build>
                <plugins>
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                      <execution>
                        <id>sources</id>
                        <phase>pre-package</phase>
                        <goals>
                          <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                          <classifier>sources</classifier>
                          <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
                          <!--the target directory won't be included, but the sources will be in the repository-->
                          <outputDirectory>${project.build.directory}/sources</outputDirectory>
                        </configuration>
                      </execution>
                      <execution>
                        <id>javadocs</id>
                        <phase>pre-package</phase>
                        <goals>
                          <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                          <classifier>javadoc</classifier>                      <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
                          <outputDirectory>${project.build.directory}/javadocs</outputDirectory>
                        </configuration>
                      </execution>
                    </executions>
                  </plugin>
                  <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.2-beta-4</version>
                    <executions>
                      <execution>
                        <phase>package</phase>
                        <goals>
                          <goal>single</goal>
                        </goals>
                        <configuration>
                          <descriptors>
                            <descriptor>src/main/assembly/archive.xml</descriptor>
                          </descriptors>
                        </configuration>
                      </execution>
                    </executions>
                  </plugin>    
                </plugins>
              </build>
            </profile>
          </profiles>
        </project>
        

        这是程序集:

        <assembly>
          <id>archive</id>
          <formats>
            <format>zip</format>
          </formats>
          <fileSets>
            <fileSet>
             <directory>${project.basedir}</directory>
             <outputDirectory>/</outputDirectory>
              <excludes>
                <exclude>target/**</exclude>
              </excludes>
            </fileSet>
            <fileSet>
             <directory>${maven.repo.local}</directory>
             <outputDirectory>repo</outputDirectory>
            </fileSet>
          </fileSets>
        </assembly>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-27
          • 1970-01-01
          相关资源
          最近更新 更多