Maven: add a folder or jar file into current classpath

前端 未结 2 1636
星月不相逢
星月不相逢 2020-12-05 18:48

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/

相关标签:
2条回答
  • 2020-12-05 19:17

    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

    0 讨论(0)
  • 2020-12-05 19:24

    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>
    
    0 讨论(0)
提交回复
热议问题