【发布时间】:2020-03-18 07:51:10
【问题描述】:
我正在使用 maven 插件创建一个 JAR。但是src/main/resources 文件夹中的所有文件都直接放置在 JAR 中,而不是 resources 文件夹中。 (根本没有在 JAR 中创建资源文件夹)
由于使用 JAR 的应用程序无法找到 log4j2.xml 文件。早期的资源文件夹正在创建,但现在不知何故它没有被创建。
pom.xml:
<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>testgroup</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<aspectj.version>1.8.10</aspectj.version>
<java.version>1.8</java.version>
<codehaus.mojo.version>1.11</codehaus.mojo.version>
<log4j.version>2.11.1</log4j.version>
<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
<maven.assembly.plugin.version>2.4.1</maven.assembly.plugin.version>
<maven.resources.plugin.version>2.6</maven.resources.plugin.version>
<sonar.maven.plugin.version>3.3.0.603</sonar.maven.plugin.version>
<artifactory.maven.plugin.version>2.6.1</artifactory.maven.plugin.version>
<eclipse.lifecycle.mapping.version>1.0.0</eclipse.lifecycle.mapping.version>
<junit.platform.surefire.provider.version>1.0.1</junit.platform.surefire.provider.version>
<maven.surefire.plugin.version>2.19.1</maven.surefire.plugin.version>
<codehaus.mojo.maven.plugin.version>1.6.0</codehaus.mojo.maven.plugin.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>package.Test</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.maven.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jfrog.buildinfo</groupId>
<artifactId>artifactory-maven-plugin</artifactId>
<version>${artifactory.maven.plugin.version}</version>
<inherited>false</inherited>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${codehaus.mojo.version}</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${codehaus.mojo.maven.plugin.version}</version>
<configuration>
<mainClass>package.Test</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.surefire.provider.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<!-- This is added to ignore error in life cycle of aspectj-maven-plugin
due to bug in m2eclipse -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>${eclipse.lifecycle.mapping.version}</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${codehaus.mojo.version}</version>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</project>
项目结构:
Project
-----src/main/java
-----src/main/resources
log4j2.xml
-----src/test/java
Jar 结构:
Jar
-----log4j2.xml
在应用程序控制台中我会收到消息:
ERROR StatusLogger Log4j2 找不到日志记录实现。 请将 log4j-core 添加到类路径中。使用 SimpleLogger 记录到 控制台...
【问题讨论】:
-
首先你的 pom 看起来你还没有意识到配置范式的约定......你应该保留源(生产代码)
src/main/java的位置以获取你应该使用的资源src/main/resources它被打包到生成的 jar 文件中。如果您需要在生成的 jar 文件中为 log4j2.xml 文件创建一个目录,只需将该目录添加到src/main/resources目录中。此外删除手动绑定 maven-compiler-plugin 到生命周期..删除 maven-surefire-plugin 中提供程序的配置(由surefire本身处理)... -
resources目录不应该是 jar 文件的一部分 ..因为约定是资源通常位于 jar 文件的根目录中……或更一般地在类路径中…… -
请通过删除
尝试一次src
标签: java maven resources pom.xml log4j2