Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从jar中排除jar生成的具有依赖项的文件_Java_Maven_Maven Assembly Plugin - Fatal编程技术网

Java 从jar中排除jar生成的具有依赖项的文件

Java 从jar中排除jar生成的具有依赖项的文件,java,maven,maven-assembly-plugin,Java,Maven,Maven Assembly Plugin,Mypom.xml包含以下内容,用于创建包含所有依赖项的项目jar。 现在我在src/main/resources中有一个属性文件,这是运行应用程序所必需的(我想从IDE开始使用它),但我不想在创建的jar文件中发布它,因为设置是单独维护的 问题:如何获取包含所有依赖项的文件,但不包括这些属性文件 maven汇编插件 做罐子 包裹 单一的 x、 主要 带有依赖项的jar 如文件规定: 如果您的项目想要将您的工件打包到uber jar中,那么组装插件只提供基本支持。要获得更多控制,请使用Mave

My
pom.xml
包含以下内容,用于创建包含所有依赖项的项目jar。
现在我在
src/main/resources
中有一个属性文件,这是运行应用程序所必需的(我想从IDE开始使用它),但我不想在创建的jar文件中发布它,因为设置是单独维护的

问题:如何获取包含所有依赖项的文件,但不包括这些属性文件


maven汇编插件
做罐子
包裹
单一的
x、 主要
带有依赖项的jar
如文件规定:

如果您的项目想要将您的工件打包到uber jar中,那么组装插件只提供基本支持。要获得更多控制,请使用Maven Shade插件


使用,您可以拥有一个胖jar(就像使用汇编插件一样),并解决通过配置排除文件的类似问题(不需要外部汇编描述符文件)

在您的情况下,要从组装的jar中排除资源,可以使用

简单的配置如下所示:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>**/*file_pattern*</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

org.apache.maven.plugins

文件。

如果您想继续使用maven assembly plugin,可以在配置过滤器的地方使用程序集描述符文件,如本节所述:

<fileSets>
<fileSet>
  <directory>${basedir}</directory>
  <includes>
    <include>*.txt</include>
  </includes>
  <excludes>
    <exclude>*.properties/exclude>
  </excludes>
</fileSet>

${basedir}
*.txt
*.属性/排除>

在maven配置pom.xml中,您可以在描述符标记中指定assemblu描述符文件(distribution.xml是一个包含上述部分的文件)


我也遇到了类似的问题(我有一个checkstyle-config.xml,不应该包含在最终组装中)。下面的解决方案对我有效:
添加资源部分使我的project-jar-with-dependencies.jar不包含checkstyle-config.xml文件

简化的pom.xml:
<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>

    <properties>
        ...
    </properties>

    <packaging>jar</packaging>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>checkstyle-config.xml</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>...</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        ...
    </dependencies>
</project>

4.0.0
...
...
...
...
罐子
${project.basedir}/src/main/resources
checkstyle-config.xml
org.apache.maven.plugins
maven汇编插件
3.3.0
带有依赖项的jar
...
组装
包裹
单一的
...

为什么要使用maven assemby插件而不是标准的maven JAR插件来构建JAR?@aribeiro因为JAR插件只打包项目工件(代码和资源),而没有依赖项,所以OP需要一个uber/fat JARinstead@A.DiMatteo谢谢你的启发。我的目光停留在“不想发送属性”部分,没有注意“依赖性”部分。谢谢。以前听说过荫凉,下一步再看@EugeneBeresovsky我刚刚意识到(我的错)您想从您自己的项目中排除一个文件,而不是从外部依赖中排除。您可能仍然使用汇编插件,请将您当前使用的汇编描述符作为问题的一部分进行分享,以便提供这两个选项。事实上,如果您想继续使用汇编插件,这是一条路,我没有注意到这是来自当前项目的文件,而不是来自外部依赖项。说得好,plus1!谢谢另外,感谢您与shade插件分享您的解决方案。将来可能会有帮助。
<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>

    <properties>
        ...
    </properties>

    <packaging>jar</packaging>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>checkstyle-config.xml</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>...</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        ...
    </dependencies>
</project>