如何使用自我可持续的maven构建创建源代码分发?

时间:2022-09-03 00:01:44

What I want to do is to create source code distribution of my application with all dependencies and burn it on DVD. So that I could build it in 100 years (well, ok, you know what I mean...). No online dependencies on libraries or maven plugins!

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

I know that Ant would be better for this, but I'm using maven in my project. I'm not going to switch to Ant just for that, I'm asking how to do this with maven. Or, if there is a way how to generate self sustainable Ant build that I could put on DVD that would be great too. (there is ant:ant plugin but it just generates Ant build.xml that points dependencies to local maven repo)

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

The approach I've taken is that I wanted to create special local repository that I can put on DVD and then build project with mvn -o -Dmaven.repo.local=repo/on/dvd. I was trying to make such repository with dependency:copy-dependencies anduseRepositoryLayout param set to true. But it doesn't copy freaking maven plugins that my build depends on...

我采取的方法是,我想创建一个特殊的本地存储库,我可以放在DVD上,然后使用mvn -o -Dmaven.repo.local = repo / on / dvd构建项目。我试图使用依赖项创建这样的存储库:copy-dependencies和useRepositoryLayout param设置为true。但它不会复制我的构建依赖的怪异的maven插件......

3 个解决方案

#1


The only way I can think of to include the plugins is to specify a different local repository for the build on the command line and ensure all the dependency sources etc are downloaded, then create an archive including the project's contents and the custom repository.

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

Here is a pom that downloads the sources and javadocs (it downloads them to the project's target directory, which we exclude from the archive because they will also be in the local repository). The assembly descriptor bundles the project's contents and the local repository into a single (pretty large) archive.

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

Note the processing is all in a profile because you really don't want this running on every build. If temporary local repository is in the target directory you can easily clean the mess up afterwards with a mvn clean. To activate the profile do something like the following:

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

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

Here's the pom:

这是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>

And here's the assembly:

这是集会:

<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>

#2


Watch this: Maven Assembly Plugin

观看:Maven Assembly插件

Quote from the homepage:

从主页引用:

Do you want to create a binary distribution from a Maven project that includes supporting scripts, configuration files, and all runtime dependencies? You need to use the Assembly Plugin to create a distribution for your project.

您是否想要从Maven项目创建二进制发行版,其中包括支持脚本,配置文件和所有运行时依赖项?您需要使用Assembly Plugin为项目创建分发。

It's well configurable. I used it especially for making self-running demo versions of web-applications with an embedded jetty server and user documentation.

它配置良好。我特别使用它来制作具有嵌入式jetty服务器和用户文档的Web应用程序的自运行演示版本。

#3


I don't have a complete answer. Last time I looked at this, I thought that cleaning out the localRepository at the start of the build (or using a separate one) and the running mvn dependency:go-offline.

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

If you're really keen, you'll also want to bundle maven itself and a JDK into the distribution. This likely takes it out of scope of a pure maven build.

如果你非常热衷,你还需要将maven本身和JDK捆绑到发行版中。这可能会超出纯maven构建的范围。

#1


The only way I can think of to include the plugins is to specify a different local repository for the build on the command line and ensure all the dependency sources etc are downloaded, then create an archive including the project's contents and the custom repository.

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

Here is a pom that downloads the sources and javadocs (it downloads them to the project's target directory, which we exclude from the archive because they will also be in the local repository). The assembly descriptor bundles the project's contents and the local repository into a single (pretty large) archive.

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

Note the processing is all in a profile because you really don't want this running on every build. If temporary local repository is in the target directory you can easily clean the mess up afterwards with a mvn clean. To activate the profile do something like the following:

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

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

Here's the pom:

这是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>

And here's the assembly:

这是集会:

<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>

#2


Watch this: Maven Assembly Plugin

观看:Maven Assembly插件

Quote from the homepage:

从主页引用:

Do you want to create a binary distribution from a Maven project that includes supporting scripts, configuration files, and all runtime dependencies? You need to use the Assembly Plugin to create a distribution for your project.

您是否想要从Maven项目创建二进制发行版,其中包括支持脚本,配置文件和所有运行时依赖项?您需要使用Assembly Plugin为项目创建分发。

It's well configurable. I used it especially for making self-running demo versions of web-applications with an embedded jetty server and user documentation.

它配置良好。我特别使用它来制作具有嵌入式jetty服务器和用户文档的Web应用程序的自运行演示版本。

#3


I don't have a complete answer. Last time I looked at this, I thought that cleaning out the localRepository at the start of the build (or using a separate one) and the running mvn dependency:go-offline.

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

If you're really keen, you'll also want to bundle maven itself and a JDK into the distribution. This likely takes it out of scope of a pure maven build.

如果你非常热衷,你还需要将maven本身和JDK捆绑到发行版中。这可能会超出纯maven构建的范围。