2010-08-04 99 views
14

我正在使用maven-compile插件来编译类。现在我想添加一个jar文件到当前的classpath中。该文件保留在另一个位置(让我说c:/jars/abc.jar。我宁愿留下这个文件在这里)。我怎样才能做到这一点。 如果我在参数中使用的类路径:Maven:将文件夹或jar文件添加到当前类路径

<configuration> 
<compilerArguments> 
    <classpath>c:/jars/abc.jar</classpath> 
</compilerArguments> 
</configuration> 

它不会工作,因为它会覆盖当前的classpath(包括所有的依赖)

请帮助我。

+0

这个罐子是否需要在那个位置?或者您是否需要一种方法来包含本地罐子? – Gamlor 2010-08-04 23:13:45

+2

重复的[Maven,如何添加额外的库不可用在回购](http://stackoverflow.com/questions/2479046/maven-how-to-add-additional-libs-not-available-in-repo), [Maven的。如何处理“无家可归”的罐子?](http://stackoverflow.com/questions/2916949/maven-what-to-do-with-homeless-jars),[本地罐子不包括在类路径中](http ://stackoverflow.com/questions/3280834/local-jars-are-not-included-in-class-path/3281409#3281409)等等。 – 2010-08-05 10:39:53

+1

您是否找到添加目录的方法?而不是在classpath中添加每个jar文件? – SJunejo 2016-08-03 02:05:12

回答

7

这可能是以前问过的。请参阅Can I add jars to maven 2 build classpath without installing them?

简而言之:将您的jar作为依赖项包含在系统范围中。这需要指定jar的绝对路径。

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

+10

我不知道什么时候人们会停止建议滥用'system'范围请参阅Brian的[答案](http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-没有安装他们/ 764684#764684)在您链接到的问题。另见[这个以前的答案](http://stackoverflow.com/questions/3280834/local-jars-are-not-included-in-class-path/3281409#3281409)。 – 2010-08-05 10:38:04

+1

还有一个问题。如果我需要将一个文件夹(包含许多.class文件)添加到classpath中。我该怎么做。 – David 2010-08-05 15:03:41

+1

@Pascal Thivent - 当人们不再像做maven central的org.bytedeco javacv-0.9项目中包含的bin.zip那样废话时,它就会结束。如果你谈论正常的maven行为,那么作为一个用户,我不能修复maven上传.....也许我应该在Maven中心抱怨。 – user1050755 2014-10-30 02:25:50

0

docsexample看看是不是清楚的类路径操作是不允许的。

<configuration> 
<compilerArgs> 
    <arg>classpath=${basedir}/lib/bad.jar</arg> 
</compilerArgs> 
</configuration> 

但见Java docs(也https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/solaris/javac.html

-classpath path指定的javac用来查找到运行javac或其他类中引用所需的类你 编译。如果已设置,则覆盖默认或CLASSPATH环境变量 。

也许有可能获得当前classpath和扩展它,
看到in maven, how output the classpath being used?

<properties> 
     <cpfile>cp.txt</cpfile> 
    </properties> 

    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.9</version> 
    <executions> 
     <execution> 
     <id>build-classpath</id> 
     <phase>generate-sources</phase> 
     <goals> 
      <goal>build-classpath</goal> 
     </goals> 
     <configuration> 
      <outputFile>${cpfile}</outputFile> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

读取文件(Read a file into a Maven property

<plugin> 
    <groupId>org.codehaus.gmaven</groupId> 
    <artifactId>gmaven-plugin</artifactId> 
    <version>1.4</version> 
    <executions> 
    <execution> 
     <phase>generate-resources</phase> 
     <goals> 
     <goal>execute</goal> 
     </goals> 
     <configuration> 
     <source> 
      def file = new File(project.properties.cpfile) 
      project.properties.cp = file.getText() 
     </source> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

最后

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>3.6.1</version> 
    <configuration> 
     <compilerArgs> 
     <arg>classpath=${cp}:${basedir}/lib/bad.jar</arg> 
     </compilerArgs> 
    </configuration> 
    </plugin> 
相关问题