【问题标题】:How to add a directory to a JAR file with Maven?如何使用 Maven 将目录添加到 JAR 文件中?
【发布时间】:2012-02-24 08:31:33
【问题描述】:

我正在尝试使用 Maven 将 Web 应用程序打包为可运行的 JAR 文件。 jar-with-dependencies 程序集负责包含所有依赖项,但我仍然需要包含 src/main/webapp

我设法使用自定义程序集将目录添加到我的 JAR:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
                              http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>webapp</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/webapp</directory>
            <outputDirectory>/webapp</outputDirectory>
        </fileSet>
    </fileSets> 
</assembly>

它确实有效,甚至可以将这个程序集与 jar-with-dependencies 一起使用。但是,最终的 JAR 包含 webapp 目录和所有依赖项,但不包含我项目的类文件。我的程序集显然正在删除它们。

我可以在程序集中保留我自己项目的类文件吗?还是有其他方法可以将目录添加到 JAR 中?

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

    我的程序集显然正在删除它们

    我猜是反过来的,它没有添加它们。

    你的类文件在 target/classes 里面,它们需要放在 target/webapp/WEB-INF/classes 里面。我猜你需要另一个这样的规则:

    <fileSet>
        <directory>target/classes</directory>
        <outputDirectory>/webapp/WEB-INF/classes</outputDirectory>
    </fileSet>
    

    【讨论】:

      【解决方案2】:

      您的类将在 ${project.build.directory}/classes(可能是/target/classes)中生成。

      因此,您应该将该文件夹用作源目录。

      尝试将&lt;directory&gt;src/main/webapp&lt;/directory&gt;改成&lt;directory&gt;{project.build.directory}/classes&lt;/directory&gt;

      【讨论】:

        【解决方案3】:

        或者,您可以在程序集描述符中添加以下内容。假设您的 maven 项目正在构建 webapp,这不仅会处理类文件,还会处理 webapp 内容。

        ...
        <dependencySets>
            <dependencySet>
                <useProjectArtifact>true</useProjectArtifact>
            </dependencySet>
        </dependencySets>
        ...
        

        【讨论】:

          猜你喜欢
          • 2020-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-26
          • 2013-11-24
          • 1970-01-01
          • 2018-03-15
          相关资源
          最近更新 更多