首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从包含txt文件的Maven项目创建JAR文件

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

Stack Overflow用户
提问于 2018-05-11 06:37:00
回答 1查看 591关注 0票数 0

我已经设法从netbeans中的maven项目创建了一个jar文件。但是,我的代码引用了我的项目中的txt文件,所以当我试图在命令提示符上执行Jar file时,我得到一个错误,指出找不到txt文件。

这是我的pom.xml文件的当前结构:

代码语言:javascript
复制
<?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 /

EN

回答 1

Stack Overflow用户

发布于 2018-05-11 06:48:22

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

From the examples page of the Resources plugin:

默认情况下,

将在src/main/resources下查找项目的资源。但是,并不是所有的资源都在src/main/resources中。因此,您必须通过将以下内容添加到POM来指定这些目录。

代码语言:javascript
复制
<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50282457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档