2017-09-25 158 views
0

我一直在尋找一種工具,它能夠向您顯示未使用的依賴關係。我很快就發現了maven命令mvn dependency:analyze。與此相關的問題是,它經常檢測到「未使用」的依賴關係,如果失敗,它會使構建失敗。mvn依賴關係中的結果不正確: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 

通過從卸下Lombok的我的POM我通過Buildig項目獲得以下錯誤

------------------------------------------------------------- 
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的,演員我得到運行項目

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

兩個Log4j和龍目以下的錯誤在我的項目中使用。你有一個想法如何解決這個問題或從哪裏來?

預先感謝您

+1

第一件事就是使用[maven-dependency-plugin]的最新版本(https://maven.apache.org/plugins/maven-dependency-plugin/)。此外,問題是如何使用這些依賴關係? – khmarbaise

+1

我相信分析器在字節碼級別運行,因此它可能不會檢測到像lombok已被編譯的未使用的內容。 –

+0

您是否嘗試修改範圍? log4j-extras可以是運行時依賴項嗎?不熟悉lombok以及如何解決這個問題 –

回答

0

我也已經運行到哪裏龍目島項目已打上了dependency:analyze與未用聲明的依賴性問題。 Lombok項目大部分時間都是在編譯時進行的,因此編譯後大部分註釋都會被刪除。這就是Maven的依賴分析器無法識別它的原因。爲什麼它也無法檢測到使用SPI加載的依賴關係。

我們通過明確告訴Dependency analyzer插件忽略Project Lombok來解決此問題。這可以通過<usedDependency>配置選項來完成,或更細粒度<ignoreUnusedDeclaredDependency>這樣:

<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:龍目島是一個編譯時的依賴,但不必在運行時類路徑(或結束在你的lib文件夾中)。 provided依賴關係指出該jar應該用於編譯,但在運行時它應該由運行時環境提供(例如運行它的容器)。由於在運行時不需要Lombok,因此當您的運行時不提供Lombok時,不會出現任何問題,這與Maven依賴聲明期望的相反。