java 带有所有依赖项和 log4j.properties 文件的 Maven jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15002299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:09:36  来源:igfitidea点击:

Maven jar with all dependencies and log4j.properties file

javamavenlog4j

提问by wojtek88

I have to build jar file with all dependencies and log4j.properties file.

我必须使用所有依赖项和 log4j.properties 文件构建 jar 文件。

I was searching for answer on stackoverflow but without luck.

我在 stackoverflow 上寻找答案,但没有运气。

For now I am building jar with all dependencies, but I do not now how can I tell to maven to move log4j.properties file to META-INF dir in ma *.jar file.

现在我正在构建具有所有依赖项的 jar,但我现在不知道如何告诉 maven 将 log4j.properties 文件移动到 ma *.jar 文件中的 META-INF 目录。

Part of my pom, that generates jar with dependencies:

我的 pom 的一部分,生成具有依赖项的 jar:

                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>MainClass</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </plugin> 

What do I have to do in order to have jar with all dependencies and log4j.properties file?

我必须做什么才能拥有包含所有依赖项和 log4j.properties 文件的 jar?

回答by Romski

It's normal to not bundle a logging file as your user may wish to configure this separately/externally. I have built a simple project with one main class and log4j in the resources folder and this works running in Netbeans.

不捆绑日志文件是正常的,因为您的用户可能希望单独/外部配置它。我在资源文件夹中构建了一个包含一个主类和 log4j 的简单项目,该项目在 Netbeans 中运行。

pom.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>com.mycompany</groupId>
  <artifactId>log4j-sample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>log4j-sample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>
</project>

src/main/com/mycompany/log4jsample/App.java

src/main/com/mycompany/log4jsample/App.java

package com.mycompany.log4jsample;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class App {
    private static Logger logger = LogManager.getLogger("HelloWorld");

    public static void main(String[] args) {
        logger.info("Hello, World!");
    }
}

/src/main/resources/log4j.properties

/src/main/resources/log4j.properties

# To change this template, choose Tools | Templates
# and open the template in the editor.

log4j.rootLogger=DEBUG, CONSOLE
#log4j.rootLogger=INFO, FILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n

output

输出

2013-02-22 00:27:39,518 INFO  - Hello, World!

回答by Can Yegane

You can use the Maven Antrun Pluginto write an ant script which will execute during a lifecycle phase of maven.

您可以使用Maven Antrun 插件编写一个 ant 脚本,该脚本将在 Maven 的生命周期阶段执行。

回答by carlspring

Simply put your log4j.propertiesunder src/main/resources/META-INF.

简单地说你log4j.propertiessrc/main/resources/META-INF

回答by wojtek88

My problem was connected to targetPath element in resources tag in pom and a :

我的问题与 pom 和 a 中资源标签中的 targetPath 元素有关:

    <resources>
        <resource>
            <targetPath>${project.build.directory}/</targetPath>
            <directory>src/main/java</directory>
            <includes>
                <include>*.properties</include>
            </includes>
        </resource>
    </resources>

All I had to do, was remove this targetPath element. Thanks for Your time guys and sorry for not pasting everything.

我所要做的就是删除这个 targetPath 元素。感谢您的时间,很抱歉没有粘贴所有内容。

回答by Dmitriy Ryabin

If you place your log4j.properties file into

如果您将 log4j.properties 文件放入

src/main/resources/log4j.properties

Then during maven build it will get placed into correct location automatically See, this is a view of my jar file.

然后在 maven 构建过程中它会自动放置到正确的位置 看,这是我的 jar 文件的视图。

enter image description here

在此处输入图片说明