2011-03-12 101 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> 
相关问题