java - 如何使用 maven 在 jar 编译中包含非 java 文件?

标签 java maven jar

我有一个包含保存数据的 .txt 文件的文件夹。这些数据被加载到我的游戏中,然后在游戏中使用。

我还有 3 个不在 Maven 上的其他库。所以我使用这个插件将它们加载到 .m2 文件夹中:maven-install-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
        <executions>
            <execution>
                <id>install-everythingrs-api-jar</id>
                <phase>validate</phase>
                <goals>
                    <goal>install-file</goal>
                </goals>
                <configuration>
                    <groupId>everythingrs-api</groupId>
                    <artifactId>everythingrs-api</artifactId>
                    <version>1.0</version>
                    <packaging>jar</packaging>
                    <file>${project.basedir}/lib/everythingrs-api.jar</file>
                    <generatePom>true</generatePom>
                </configuration>
            </execution>
            <execution>
                ...
            </execution>
            <execution>
                ...
            </execution>
        <executions>
    </plugin> 

然后我使用这个插件:maven-assemble-plugin 将项目编译成一个包含其中库的 jar。

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>${main-class-path}</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>

现在,如果我运行 mvn clean validate install,会将我拥有的 3 个库复制到 .m2 中,然后第二个插件将编译成一个可执行 jar,其中包含从 maven repo 导入的库以及添加到的 3 个库将.m2放入 jar 中。

但是,我还有一个位于项目 ./中但在 src 之外的文件夹,我怎样才能使 Maven 插件也将此目录/文件夹包含到 jar 中?

最佳答案

尝试使用maven resources plugin.它允许您添加不在 /src/main/resources 下的文件夹作为 Maven 资源。

关于java - 如何使用 maven 在 jar 编译中包含非 java 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61257533/

相关文章:

java - 用于在多个不同的 javac 任务中指定源/版本的 Ant 快捷方式

maven - 具有默认访问权限的类会导致 java 11 的 Spring Boot 项目在运行时出现 NoClassDefFound 错误

java - Jar 文件无法运行,提取的类运行正常

java - 从另一个尚未加载的 JAR 加载资源

java - 无法在JSP代码中导入jar文件

java - 为什么我的 Java 应用程序找不到我的属性文件

java - Eclipse IAutoEditStrategy 仅在退格时调用;如何让它调用所有编辑?

java - Intellij 构建错误消息无法理解

jakarta-ee - struts2 junit 2.3.12 插件 - 无法在 struts2 junit4 中编写成功测试

maven - "mvn install"应该在集成测试时失败吗?