从包含txt文件的Maven项目创建JAR文件

时间:2018-05-10 22:37:00

标签: java maven netbeans

我已经设法从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 /

1 个答案:

答案 0 :(得分:2)

要将文件包含到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>
相关问题