2017-08-11 31 views
-2

使用的IntelliJ IDEA(版本2017年2月1日)我在這我想包括slf4jslf4j-binding一個Java/Maven項目內。如何包括SLF4J-簡單的神器JAR當構建從IntelliJ IDEA的

我知道StackOverflow是關於slf4j及其缺少綁定的大量問題,但大多數都是指Eclipse。然而,我的問題發生在Intellij下。

pom.xml我列出<dependencies>下:

<dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-api</artifactId> 
    <version>1.7.25</version> 
</dependency> 
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> 
<dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-simple</artifactId> 
    <version>1.7.25</version> 
</dependency> 

現在,當我讓的IntelliJ打造神器JAR和我然後運行命令行,我得到了(可怕的)錯誤的JAR:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 

經過檢查,JARdoes確實不包含slf4j-simple類(slf4j類雖然存在)。我該如何解決這個問題,指示Intellij合併slf4j-simple作爲列表離子pom.xml

爲了完整起見,這裏完整的項目pom.xml的IntelliJ使用:

<?xml version="1.0" encoding="UTF-8"?> 
<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>au.gov.acic.travelalert</groupId> 
    <artifactId>extract-data</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 


    <dependencies> 
     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.7.25</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-simple</artifactId> 
      <version>1.7.25</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> 
     <dependency> 
      <groupId>commons-beanutils</groupId> 
      <artifactId>commons-beanutils</artifactId> 
      <version>1.9.3</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-lang3</artifactId> 
      <version>3.6</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-collections4</artifactId> 
      <version>4.1</version> 
     </dependency>  <dependency> 
      <!-- jsoup HTML parser library @ https://jsoup.org/ --> 
      <groupId>org.jsoup</groupId> 
      <artifactId>jsoup</artifactId> 
      <version>1.10.3</version> 
     </dependency> 
    </dependencies> 

</project> 

最後一點:剛開始時,我成立了項目記錄甚至沒有工作的IDE內,儘管pom.xml中有正確的條目。但不知何故,在爲slf4j-log4j12額外添加(並隨後刪除)依賴項之後,IDE會挑選slf4j-simple,但在寫出製品JAR時仍然不會將其捆綁在一起...

+0

您使用的是maven命令?你的項目中是否有其他的依賴項,並且slf4j是唯一一個沒有在你的jar文件中結束的項目? – Michael

+0

如果您正在用'maven'構建'.jar',IDE就無關緊要。你應該發佈'.pom'中的所有相關部分,因爲當你執行'mvn package'時不包括它' –

+0

@Michael我自己沒有發佈maven命令 - 我讓Intellij使用* artefact - >建立*菜單。 – halloleo

回答

0

問題是IntelliJ的JAR工件XML配置不包含slf4j-simple庫。

這解釋了IntelliJ找到了lib射線(根據maven根據POM/XML〜下載),並將其用於項目的內部執行,但它並未將其打包到JAR中。

的解決方案是將libarary添加到XML配置,

  • 手動或者,通過編輯XML文件。 (你會發現它[projectroot].idea/artifacts/[projectname_jar.xml下)

  • 通過的IntelliJ的圖形用戶界面,通過打開文物對話框(文件 - >項目結構 - >文物),然後加入天秤座到輸出佈局列表。
-1

您可以嘗試此解決方案;去home目錄並刪除.m2目錄(隱藏)更新maven項目並嘗試重建。並且如果你正在使用(Indigo,Juno和Kepler)版本的eclipse,如果你想在其中使用最新版本或者stil,那麼你也可以檢查你的eclipse版本,然後你可以參考下面的例子;此stackoverflow鏈接在這個問題上有最可靠的答案:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". error問題Konstantinos Margaritis和許多java專家的回答。

+1

正如問題中提到的,我不*使用Eclipse。 – halloleo

-1

執行MVN依賴性:樹和檢查:

  • SLF4J的API和SLF4J-簡單的必須存在那裏。
  • 沒有其他slf4j結合庫存在
  • 可以存在slf4j橋(以庫名稱後綴爲「-slf4j」後綴的橋;例如,log4j-over-slf4j)。
  • 確保所有slf4j庫版本匹配
  • 確保不存在具有不同版本的重複slf4j庫。

如果所有這些都沒問題,沒有理由在工件上存在slf4j-api,但沒有slf4j-simple。你在做什麼來打包jar?

+0

我自己並沒有發佈maven命令 - 我讓Intellij使用artefact - > build菜單執行此操作。我將嘗試瞭解我如何在Intellij中發佈這個消息 - 或者你知道嗎? – halloleo

+0

爲什麼是負面的?無論如何,是的,我知道你在使用intellij。有什麼不同?你可以執行任何maven目標:https://stackoverflow.com/questions/27246065/how-to-execute-mvn-command-using-intellij-idea –

相關問題