2013-03-31 44 views
1

我有一個由Maven管理的實驗項目。它使用庫來生成PDF。這是我的pom.xmlMaven在編譯類型中找不到依賴關係

<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>br.com.brandizzi.adam.pdfize</groupId> 
    <artifactId>pdfize</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.xhtmlrenderer</groupId> 
      <artifactId>core-renderer</artifactId> 
      <version>R8pre2</version> 
      <scope>runtime</scope> 
     </dependency> 
    </dependencies> 
</project> 

它只有一類:

package br.com.brandizzi.adam.pdfize; 
// 

public class Main { 

    public static void main(String[] args) throws DocumentException, 
      MalformedURLException, FileNotFoundException { 

     String inputUrl = new File(args[0]).toURI().toURL().toString(); 
     OutputStream pdf = new FileOutputStream(args[1]); 

     ITextRenderer renderer = new ITextRenderer(); 
     renderer.setDocument(inputUrl); 
     renderer.layout(); 
     renderer.createPDF(pdf); 

    } 
} 

當我運行mvn compile,但是,我得到這個錯誤:

$ mvn compile 
[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building pdfize 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ pdfize --- 
[debug] execute contextualize 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ pdfize --- 
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! 
[INFO] Compiling 1 source file to /home/adam/software/workspaces/test/pdfize/target/classes 
[INFO] ------------------------------------------------------------- 
[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[9,28] error: package org.xhtmlrenderer.pdf does not exist 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[11,23] error: package com.lowagie.text does not exist 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[38,3] error: cannot find symbol 
[ERROR] class Main 
/home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[29,11] error: cannot find symbol 
[ERROR] class Main 
/home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[43,2] error: cannot find symbol 
[ERROR] class Main 
/home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[43,31] error: cannot find symbol 
[INFO] 6 errors 
[INFO] ------------------------------------------------------------- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 1.094s 
[INFO] Finished at: Sat Mar 30 21:36:59 BRT 2013 
[INFO] Final Memory: 9M/106M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project pdfize: Compilation failure: Compilation failure: 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[9,28] error: package org.xhtmlrenderer.pdf does not exist 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[11,23] error: package com.lowagie.text does not exist 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[38,3] error: cannot find symbol 
[ERROR] class Main 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[29,11] error: cannot find symbol 
[ERROR] class Main 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[43,2] error: cannot find symbol 
[ERROR] class Main 
[ERROR] /home/adam/software/workspaces/test/pdfize/src/main/java/br/com/brandizzi/adam/pdfize/Main.java:[43,31] error: cannot find symbol 
[ERROR] -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

到底哪裏出問題了?

回答

3

問題是,當應使用compile作用域時,依賴關係配置設置爲runtime作用域。因此,在pom.xml<dependency>元素應該改成是這樣的:

<dependencies> 
    <dependency> 
     <groupId>org.xhtmlrenderer</groupId> 
     <artifactId>core-renderer</artifactId> 
     <version>R8pre2</version> 
     <scope>compile</scope> <!-- <== NOTE THE DIFFERENCE --> 
    </dependency> 
</dependencies> 

當然,這提出了一個問題關於爲什麼我會嘗試使用runtime範圍在首位。我試圖構建一個具有所有依賴關係的JAR,如果您也嘗試這樣做,那麼改變範圍就沒有意義了。相反,請按照此awesome answer

+5

在這種情況下,你實際上可以不指定範圍('compile'是默認值)。 – Daniel