从 Maven 项目生成发行版

发布于 2024-12-05 20:30:55 字数 392 浏览 1 评论 0原文

我想用 Maven 构建一个应用程序发行版。我的项目中有一个模块,其中包含主要源代码。我决定从这个模块构建应用程序发行版。我在模块的 POM 中告诉 maven 将所有配置文件和依赖库复制到 target/ 目录。我的问题是 Maven 将所有与构建相关的临时目录(如。classesgenerated-sourcesmaven-archiver)保留在目标目录中。我不想至少在安装阶段自动删除它们。我怎样才能实现这个目标?如果我将 maven-clean-plugin 放在构建的末尾,看起来 Maven 总是会删除整个 target 目录,无论我试图排除谁的文件我想保留的东西。

I want to build an application distribution with Maven. I have a module in my project which holds the main source code. I decided to build the application distribution from this module. I'm telling maven in the module's POM to copy all the configuration files and dependant libs to the target/ directory. My problem is that Maven keeps all the build related temporary dirs (like. classes, generated-sources, maven-archiver) in the target directory. I wan't to auto delete these at least during the install phase. How can i achive this? If i put the maven-clean-plugin to the end of the build it looks like Maven always deletes the whole target directory, no matter who i'm trying to exclude files what i'm trying to keep.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-12-12 20:30:55

在你的 pom 中尝试一下

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <executions>
        <execution>
            <id>user_distribution</id>
            <phase>package</phase>
            <goals>
                <goal>attached</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/dist.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

,这是 xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/xsd/assembly-1.1.1.xsd">

    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>
    <files>
        <file>
            <source>target/${pom.artifactId}-${pom.version}.jar</source>
            <outputDirectory>lib/</outputDirectory>
        </file>
    </files>
    <fileSets>
        <fileSet>
            <directory>directory to be included</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>file name to be included</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>another directory to be included</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

try this in your pom

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <executions>
        <execution>
            <id>user_distribution</id>
            <phase>package</phase>
            <goals>
                <goal>attached</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/dist.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

and here is the xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/xsd/assembly-1.1.1.xsd">

    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>
    <files>
        <file>
            <source>target/${pom.artifactId}-${pom.version}.jar</source>
            <outputDirectory>lib/</outputDirectory>
        </file>
    </files>
    <fileSets>
        <fileSet>
            <directory>directory to be included</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>file name to be included</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>another directory to be included</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>
梦太阳 2024-12-12 20:30:55

我在模块的POM中告诉maven复制所有配置
文件和依赖库到目标/目录

你是怎么做的?

根据这个问题,看起来您不需要担心有选择地删除目标文件夹的内容(这可以通过自定义 maven clean 插件),但使用 创建发行版maven 程序集插件 (如所指出的来自@Scorpion)。

后者允许您将所有项目工件(包括依赖项)捆绑在一起,形成 zip 或其他格式,然后开发人员可以轻松使用。您可能希望通过使用单独的配置文件将其与常规构建分离,以便仅根据需要构建发行版。

I'm telling maven in the module's POM to copy all the configuration
files and dependant libs to the target/ directory

How are you doing this?

Based on the question, it looks like you need to worry less about selectively deleting contents of target folder (which can be done by customizing maven clean plugin), but creating a distribution using maven assembly plugin (as pointed out by @Scorpion).

The latter allows you to bundle together all your project artifacts including the dependencies into zip or other formats, which can then be easily used by developers. You may want to decouple this from regular build by using a separate profile, so that the distribution is built only on need basis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文