You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven打包JAR后logback日志未生成,SBT打包正常求助

解决Maven打包JAR后Logback日志文件不生成的问题

看起来你遇到的问题是Maven Assembly打包的JAR运行时,Logback配置的日志文件没生成,但SBT Assembly却能正常工作。我来帮你分析下可能的原因和解决办法:

首先排查核心问题:Logback配置文件是否被正确打包

最常见的原因就是logback.xml没被包含到最终的JAR包里,导致Logback启动时找不到配置,只能用默认的控制台输出,自然不会生成文件。你可以先验证这一点:

  • 执行打包命令:mvn clean package
  • 然后运行这个命令检查JAR里是否有logback.xml:
    jar tf target/myTest.jar | grep logback.xml
    

如果没有任何输出,说明资源文件确实没被打包进去,这就是问题根源。

解决方案1:调整Maven Assembly插件的配置,强制包含资源文件

你的pom.xml里用的是jar-with-dependencies这个默认描述符,它可能没有正确包含src/main/resources下的资源文件。你需要在Assembly插件的配置里添加fileSets,明确指定要包含这些资源:

修改你的maven-assembly-plugin配置,在<configuration>标签内加入:

<fileSets>
  <fileSet>
    <directory>src/main/resources</directory>
    <outputDirectory>/</outputDirectory>
    <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include>
    </includes>
  </fileSet>
</fileSets>

完整的Assembly插件配置会变成这样:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>build-a</id>
      <configuration>
        <archive>
          <manifest>
            <mainClass>org.my.Test</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <fileSets>
          <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
              <include>**/*.xml</include>
              <include>**/*.properties</include>
            </includes>
          </fileSet>
        </fileSets>
        <finalName>myTest</finalName>
      </configuration>
      <phase>package</phase>
      <goals>
        <goal>assembly</goal>
      </goals>
    </execution>
  </executions>
</plugin>

这个配置会把src/main/resources下的所有xml和properties文件打包到JAR的根目录,这样Logback就能在类路径下找到它的配置文件了。

解决方案2:换成Maven Shade插件(更适合Scala项目)

有时候Maven Assembly插件在处理Scala项目的资源和依赖时会有兼容性问题,换成Shade插件往往能解决这类问题。Shade默认会包含target/classes下的所有文件(包括资源),而且对依赖的合并处理更友好。

把你原来的maven-assembly-plugin替换成以下配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.4.1</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>org.my.Test</mainClass>
          </transformer>
          <!-- 可选:处理可能的资源冲突,比如多个JAR里的服务配置 -->
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/javax.servlet.ServletContainerInitializer</resource>
          </transformer>
        </transformers>
        <finalName>myTest</finalName>
      </configuration>
    </execution>
  </executions>
</plugin>

最后验证

修改配置后,重新执行mvn clean package,然后运行java -jar target/myTest.jar,再去/my/path/目录下检查,应该就能看到myLog.log文件了。

如果还是有问题,你可以检查下Logback的依赖是否正确引入(确保logback-classic和slf4j-api在pom.xml的依赖里),不过既然SBT打包正常,这一步大概率没问题。

内容的提问来源于stack exchange,提问作者Markus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:28:49