无法在Intellij中为Maven项目的最终jar添加资源

无法在Intellij中为Maven项目的最终jar添加资源

本文介绍了无法在Intellij中为Maven项目的最终jar添加资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将资源目录(标记为资源根目录)中的图像添加到要生成的最终jar中.我试过使用File-> Project Structure-> Project Settings-> Artifacts->单击绿色加号-> Jar-> From具有依赖项的模块来构建jar.但是失败了.我也尝试过从Intellij的Maven项目窗口构建一个jar.哪个也失败了.

I am unable to add images from my resources directory (which is marked as resources root) to the final jar that I want t generate. I have tried building a jar using File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies. But failed. I have also tried building a jar from the Maven project window in Intellij. Which failed as well.

这是我的pom.xml文件:

Here is my pom.xml file:

<?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.sample.project</groupId>
<artifactId>login</artifactId>
<version>1.0-SNAPSHOT</version>


<build>
    <resources>
        <resource>
            <directory>
                src/main/resources
            </directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.sample.project.Login</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

我的项目结构如下:项目结构

zip文件:存档

推荐答案

目录 src/resources/中的文件需要移动到 src/main/resources/,否则Maven将忽略它们.

The files that you have in the directory src/resources/ need to be moved to src/main/resources/, otherwise Maven will ignore them.

另外,删除此

    <resources>
        <resource>
            <directory>
                src/main/resources
            </directory>
        </resource>
    </resources>

因为这是Maven仍将使用的默认约定.

because this is the default convention that Maven will use anyway.

这篇关于无法在Intellij中为Maven项目的最终jar添加资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:24