2011-03-12 99 views
14

我有一個項目使用「系統」範圍來指定一個jar文件,包括在我的項目的WEB-INF/lib目錄中。這個工件不在任何maven倉庫中,所以我必須將它包含在我的項目中。我這樣做如下:Maven exec插件 - 如何包含「系統」類路徑?

<dependency> 
     <groupId>com.example</groupId> 
     <artifactId>MySpecialLib</artifactId> 
     <version>1.2</version> 
     <scope>system</scope> 
     <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/MySpecialLib-1.2.jar</systemPath> 
    </dependency> 

這對大多數事情都很好。

但現在我試圖運行在命令行中的一些代碼(我的web應用程序之外,通過main()方法我已經加入),因爲它不是在「運行」收錄mvn exec:java無法解決MySpecialLib代碼類路徑。

如何,我不是:

  • 添加MySpecialLib到運行時類路徑

  • 告訴mvn exec:java也使用system類路徑?

我試過mvn exec:java -Dexec.classpathScope=system,但是這樣就會遺漏所有runtime

回答

1

有趣的是知道classpathScope=system下降runtime依賴關係。我發現通過將其作爲plugin包含在pom.xml中作爲替代方案。你能否請讓我知道它是否也適用於你?

所以我增加了一個系統級依賴於公共收集作爲一個例子就像你有你的神器: -

<dependency> 
     <groupId>commons-collections</groupId> 
     <artifactId>commons-collections</artifactId> 
     <version>3.0</version> 
     <scope>system</scope> 
     <systemPath>C:\\<some_path>\\commons-collections-3.0.jar</systemPath> 
    </dependency> 

然後在<build>標籤我已在exec-maven-plugin插件將在install階段執行: -

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.1</version> 
    <executions> 
    <execution> 
    <phase>install</phase> 
    <goals> 
     <goal>java</goal> 
    </goals> 
    <configuration> 
     <mainClass>com.stackoverflow.test.App</mainClass> 
    </configuration> 
    </execution> 
    </executions> 
    </plugin> 

然後我跑mvn install。我也確保com.stackoverflow.test.App類有一些代碼調用commons-collections-3.0的類。

希望這會有所幫助。

0

正確的答案是使用maven-install-plugin並將Jar放入本地回覆。或者,更好的辦法是運行nexus或ar​​tifactory,並使用deploy插件將jar放到那裏。系統類路徑只是一個受到傷害的世界。

+2

它添加到你的本地回購的問題是,它的視線,心不煩 - 容易忘記它是你添加到你的版本的自定義內容,並且容易丟失。您的團隊中的其他開發人員也必須被指示執行相同的手動安裝。 Nexus是一個巨大的解決方案,它應該是一個簡單的問題 - 爲您的構建添加定製的工件。然而,我同意你在受傷的世界。 :-) – 2011-03-13 14:11:34

+0

我已經寫了makefiles腳本安裝到本地回購,使其令人難忘。另外,在我看來,免費聯結是一個小錘子,提供了豐富的有用功能。 – bmargulies 2011-03-13 15:27:30

13

使用 '編譯' 範圍,運行Maven Exec插件 - mvn exec:java -Dexec.classpathScope=compile。這將包括系統範圍的依賴關係。

0

正如E.G.指出,解決方案是在運行exec時使用編譯範圍。

在每次調用:

mvn exec:java -Dexec.classpathScope=compile 

或直接在EXEC-插件配置:

 <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>exec-maven-plugin</artifactId> 
     ... 
     <configuration> 
       <classpathScope>compile</classpathScope> 
     </configuration> 
    </plugin> 
相關問題