Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: add a folder or jar file into current classpath

Tags:

I am using maven-compile plugin to compile classes. Now I would like to add one jar file into the current classpath. That file stays in another location (let's say c:/jars/abc.jar . I prefer to leave this file here). How can I do that?

If I use classpath in the argument:

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

it will not work because it will override the current classpath (that includes all the dependencies)

like image 592
David Avatar asked Aug 04 '10 23:08

David


People also ask

Does maven add to classpath?

Maven Archiver can add the classpath of your project to the manifest.

How do I add a folder to my class path?

In the section System Variables, find the CLASSPATH environment variable and select it. Click Edit. If the CLASSPATH environment variable does not exist, click New . Add all folders separated with separator.


2 Answers

This might have been asked before. See Can I add jars to maven 2 build classpath without installing them?

In a nutshell: include your jar as dependency with system scope. This requires specifying the absolute path to the jar.

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

like image 103
michid Avatar answered Sep 19 '22 13:09

michid


From docs and example it is not clear that classpath manipulation is not allowed.

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

But see Java docs (also https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/solaris/javac.html)

-classpath path Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set.

Maybe it is possible to get current classpath and extend it,
see 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 file (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> 

and finally

  <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> 
like image 21
Paul Verest Avatar answered Sep 17 '22 13:09

Paul Verest