【问题标题】:Creating a JAR File from a Maven Project that contains txt files从包含 txt 文件的 Maven 项目创建 JAR 文件
【发布时间】:2018-05-10 22:37:00
【问题描述】:

我已经设法从 netbeans 中的 maven 项目创建了一个 jar 文件。但是,我的代码引用了我项目中的 txt 文件,因此当我尝试在命令提示符下执行我的 Jar 文件时,我收到一条错误消息,提示它找不到 txt 文件。

这是我的 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>com.mycompany</groupId>
    <artifactId>mavenproject3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
   </properties>

  <build>
      <plugins>
     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- MainClass in mainfest make a executable jar -->
                <archive>
                  <manifest>
                    <mainClass>com.mycompany.mavenproject3.SSBStub</mainClass>
                  </manifest>
                </archive>

            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <!-- bind to the packaging phase -->
                <phase>package</phase> 
                <goals>
                    <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>
      </plugins>
  </build>  
    <dependencies>   
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock-standalone</artifactId>
            <version>2.17.0</version>
        </dependency>
    </dependencies>
</project>

我可以对 pom.xml 文件做些什么来消除这个错误吗?

目前我的txt文件在以下目录:src/text/__files/

【问题讨论】:

  • 将该文件或文件移动到src/main/resources 目录中,它们将自动打包到生成的jar文件中...

标签: java maven netbeans


【解决方案1】:

要将文件包含到您的 JAR 中,您可以使用 Maven Resource Plugin。它基本上从一个目录(默认为src/main/resources)中获取所有文件并将它们放入您的输出 JAR。

From the examples page of the Resources plugin:

默认情况下,Maven 会在下面查找您项目的资源 src/main/resources。但是,您的所有资源可能不在 src/main/resources。因此,您必须通过以下方式指定这些目录 将以下内容添加到您的 POM。

<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>

【讨论】:

  • 所以,我的文件当前位于以下文件夹中:src/test/resources/__files,那么我究竟在 标记中传递了什么?我有点不确定如何传递值
  • 我不确定如果我做得正确,我传入了以下值:src\test\resources\__files 但我仍然收到错误。它似乎仍然无法找到该文件。如果有帮助,它似乎正在以下目录中寻找文件:C:\Users\xxx\Documents\NetBeansProjects\mavenproject3\target
  • Maven 使用正斜杠作为文件路径
  • 没什么,因为你使用的是默认目录,所以你不需要配置一些东西。这是Convention over Configuration 范式。
  • @khmarbaise,我不太明白
猜你喜欢
  • 1970-01-01
  • 2023-02-12
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 2017-09-12
  • 1970-01-01
  • 2016-11-17
相关资源
最近更新 更多