【问题标题】:Incorrect results in mvn dependency:analyzemvn 依赖的错误结果:分析
【发布时间】:2018-03-06 13:20:03
【问题描述】:

我一直在寻找一种工具,它能够向您显示未使用的依赖项。我很快就偶然发现了 maven 命令mvn dependency:analyze。这样做的问题是,它经常检测到“未使用”的依赖项,如果缺少这些依赖项,就会导致构建失败。

这是一个优化项目的示例:

$ mvn dependency:analyze

[INFO] Building LogfileTool 0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) > test-compile @ LogfileTool >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ LogfileTool ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ LogfileTool ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to C:\Projects\LogfileToolMa\LogfileTool\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ LogfileTool ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Projects\LogfileToolMa\LogfileTool\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ LogfileTool ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) < test-compile @ LogfileTool <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ LogfileTool ---
[WARNING] Unused declared dependencies found:
[WARNING]    log4j:apache-log4j-extras:jar:1.2.17:compile
[WARNING]    org.projectlombok:lombok:jar:1.16.18:provided
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

Pom.xml 中的依赖项

<dependencies>

<!-- Logger -->
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>apache-log4j-extras</artifactId>
  <version>1.2.17</version>
</dependency>


<!-- Generating Getter, Setter etc. -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.18</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
  <type>jar</type>
</dependency>

</dependencies>

龙目岛使用过的包有:

lombok.AccessLevel
lombok.Data
lombok.Getter
lombok.Setter

通过从我的 POM 中删除 lombok,我通过 Buildig the Project 得到以下错误

-------------------------------------------------------------
COMPILATION ERROR : 
-------------------------------------------------------------
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[7,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[8,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[9,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[10,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[26,2] cannot find symbol
  symbol: class Data
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[13,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[14,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[15,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[16,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[26,2] cannot find symbol
  symbol: class Data
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[34,4] cannot find symbol
  symbol:   class Getter
  location: class  ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[35,4] cannot find symbol
  symbol:   class Setter
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[35,4] cannot find symbol
  symbol:   class Getter
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[36,4] cannot find symbol
  symbol:   class Setter
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[62,7] cannot find symbol
  symbol:   method setXmlToRead(java.lang.String)
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[63,7] cannot find symbol
  symbol:   method setPathToLogfiles(java.lang.String)
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[63,7] cannot find symbol
  symbol:   method setPathForStorage(java.lang.String)
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[64,7] cannot find symbol
  symbol:   method setPathToLogfile(java.lang.String)
  location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/log_handler/LogfileReader.java:[93,50] cannot find symbol
  symbol:   method getPathToLogfiles()
  location: variable logfileSearchConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/log_handler/LogfileReader.java:[95,100] cannot find symbol
  symbol:   method getXmlToRead()
  location: variable logfileSearchConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/Main.java:[61,51] cannot find symbol
  symbol:   method getPathToLogfile()
  location: variable logfileToolConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/Main.java:[71,52] cannot find symbol
  symbol:   method getPathForStorage()
  location: variable logfileToolConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
22 errors 
-------------------------------------------------------------
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 1.747 s
Finished at: 2017-09-27T10:04:20+02:00
Final Memory: 14M/210M
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project LogfileTool: Compilation failure: Compilation failure:
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[7,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[8,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[9,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[10,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[26,2] cannot find symbol
symbol: class Data
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[13,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[14,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[15,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[16,14] package lombok does not exist
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[26,2] cannot find symbol
symbol: class Data
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[34,4] cannot find symbol
symbol:   class Getter
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[35,4] cannot find symbol
symbol:   class Setter
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[35,4] cannot find symbol
symbol:   class Getter
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[36,4] cannot find symbol
symbol:   class Setter
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[62,7] cannot find symbol
symbol:   method setXmlToRead(java.lang.String)
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileSearchConfig.java:[63,7] cannot find symbol
symbol:   method setPathToLogfiles(java.lang.String)
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[63,7] cannot find symbol
symbol:   method setPathForStorage(java.lang.String)
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/config_handler/LogfileParseConfig.java:[64,7] cannot find symbol
symbol:   method setPathToLogfile(java.lang.String)
location: class ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/log_handler/LogfileReader.java:[93,50] cannot find symbol
symbol:   method getPathToLogfiles()
location: variable logfileSearchConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/log_handler/LogfileReader.java:[95,100] cannot find symbol
symbol:   method getXmlToRead()
location: variable logfileSearchConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileSearchConfig
ch/glue/logfiletool/logfiletool/Main.java:[61,51] cannot find symbol
symbol:   method getPathToLogfile()
location: variable logfileToolConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
ch/glue/logfiletool/logfiletool/Main.java:[71,52] cannot find symbol
symbol:   method getPathForStorage()
location: variable logfileToolConfig of type ch.glue.logfiletool.logfiletool.config_handler.LogfileParseConfig
-> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

如果没有 log4j-extras,我会通过运行项目得到以下错误

log4j:ERROR Could not instantiate class [org.apache.log4j.rolling.RollingFileAppender].
java.lang.ClassNotFoundException: org.apache.log4j.rolling.RollingFileAppender 

log4j 和 lombok 都在我的项目中使用。你有什么想法可以解决这个问题或这是从哪里来的吗?

提前谢谢你

【问题讨论】:

  • 首先是使用最新版本的maven-dependency-plugin。此外,问题是如何使用这些依赖项?
  • 我相信分析器在字节码级别运行,因此它可能无法检测到像 lombok 这样的东西因为它已经编译而未被使用。
  • 您是否尝试过修改范围? log4j-extras 可以成为运行时依赖项吗?不熟悉龙目岛以及如何解决这个问题
  • @VivekChavda 我没有修改范围,因为代码正在运行
  • @Cruxlp 您是否有一个 .xml 或 .properties,其中包含提及 RollingFileAppender 的 log4j 配置,并且在您的代码中配置为默认日志配置?

标签: java maven dependencies


【解决方案1】:

我也遇到了 Lombok 项目被 dependency:analyze 标记为未使用声明依赖项的问题。 Project Lombok 完成了大部分神奇的编译时间,因此大多数注释在编译后被删除。这就是为什么 Maven 的依赖分析器无法识别它的原因。以及为什么它也无法检测到使用 SPI 加载的依赖项。

我们通过明确告诉依赖分析器插件忽略 Project Lombok 来解​​决这个问题。这可以通过&lt;usedDependency&gt; 配置选项或更细粒度的&lt;ignoreUnusedDeclaredDependency&gt; 来完成:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <ignoredUnusedDeclaredDependencies>
                    <ignoredUnusedDeclaredDependency>org.projectlombok:lombok</ignoredUnusedDeclaredDependency>
                </ignoredUnusedDeclaredDependencies>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

关于范围的 PS:Lombok 是一个编译时依赖项,但在运行时不需要位于类路径中(或最终位于您的 lib 文件夹中)。 provided 依赖项声明 jar 应该存在以进行编译,但在运行时它应该由运行时环境提供(例如,您运行它的容器)。由于运行时不需要 Lombok,因此当您的运行时不提供 Lombok 时不会出现问题,这与您对 Maven 依赖项声明的期望相反。

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2018-02-22
    • 2013-07-10
    • 2018-08-17
    相关资源
    最近更新 更多