Maven 2 Maven:将文件夹或jar文件添加到当前类路径中

Maven 2 Maven:将文件夹或jar文件添加到当前类路径中,maven-2,jar,classpath,Maven 2,Jar,Classpath,我正在使用maven编译插件来编译类。现在我想在当前的类路径中添加一个jar文件。该文件保存在另一个位置(比如c:/jars/abc.jar。我更愿意将该文件留在这里)。我该怎么做 如果我在参数中使用classpath: <configuration> <compilerArguments> <classpath>c:/jars/abc.jar</classpath> </compilerArguments> </conf

我正在使用maven编译插件来编译类。现在我想在当前的类路径中添加一个jar文件。该文件保存在另一个位置(比如c:/jars/abc.jar。我更愿意将该文件留在这里)。我该怎么做

如果我在参数中使用classpath:

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

c:/jars/abc.jar
它将不起作用,因为它将覆盖当前类路径(包括所有依赖项)


请帮帮我。

这可能是以前问过的。看

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

另请参见中的,不清楚是否不允许类路径操作

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

(还)

-classpath指定javac用于查找运行javac所需的类或正在被其他类引用的类的路径 编译。重写默认或CLASSPATH环境变量 如果设置好了

也许可以获取当前类路径并对其进行扩展,


cp.txt
org.apache.maven.plugins
)


org.codehaus.gmaven
gmaven插件
1.4
产生资源
执行
def文件=新文件(project.properties.cpfile)
project.properties.cp=file.getText()
最后

  <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>

org.apache.maven.plugins
maven编译器插件
3.6.1
classpath=${cp}:${basedir}/lib/bad.jar

编译器插件的类路径设置是两个参数。这样改变了它,它对我起了作用:

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

org.apache.maven.plugins
maven编译器插件
3.6.1
-cp
${cp}:${basedir}/lib/bad.jar
我使用gmavenplus插件读取路径并创建属性“cp”:

<plugin>
        <!--
          Use Groovy to read classpath and store into
          file named value of property <cpfile>

          In second step use Groovy to read the contents of
          the file into a new property named <cp>

          In the compiler plugin this is used to create a
          valid classpath
        -->
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.12.0</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>3.0.6</version>
            <type>pom</type>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>read-classpath</id>
            <phase>validate</phase>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>

        </executions>
        <configuration>
          <scripts>
            <script><![CDATA[
                    def file = new File(project.properties.cpfile)
                    /* create a new property named 'cp'*/
                    project.properties.cp = file.getText()
                    println '<<< Retrieving classpath into new property named <cp> >>>'
                    println 'cp = ' + project.properties.cp
                  ]]></script>
          </scripts>
        </configuration>
      </plugin>

org.codehaus.gmavenplus
gmavenplus插件
1.12.0
org.codehaus.groovy
groovy all
3.0.6
聚甲醛
运行时
读取类路径
验证
执行

这个jar需要精确地位于那个位置,还是只需要一种包含本地jar的方法?重复的,以及许多其他的。您找到添加dir的方法了吗?我不知道人们什么时候会停止建议滥用
系统
范围,而不是在classpath中添加每个单独的jar?请参见您链接的问题中的Brian。另见,还有一个问题。如果我需要将文件夹(包含许多.class文件)添加到类路径中。“我该怎么做呢?”Pascal Thivent——好吧,当人们不再做像maven central的org.bytedeco javacv-0.9项目中的bin.zip这样的废话时,它就会结束。如果你谈论正常的maven行为,那么,作为一个用户,我无法修复maven上传。。。。。也许我应该向maven central投诉。不幸的是,这对JAR或类的文件夹或目录不起作用!令人惊讶的是,没有简单的方法可以将非maven依赖项的外部文件夹添加到maven的类路径中。这里的方法似乎都不起作用,它们失败的标志无效:classpath=/pathtojar.jar:/anotherjar.jar
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
      <compilerArgs>
         <arg>-cp</arg>
         <arg>${cp}:${basedir}/lib/bad.jar</arg>
      </compilerArgs>
    </configuration>
   </plugin>
<plugin>
        <!--
          Use Groovy to read classpath and store into
          file named value of property <cpfile>

          In second step use Groovy to read the contents of
          the file into a new property named <cp>

          In the compiler plugin this is used to create a
          valid classpath
        -->
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.12.0</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>3.0.6</version>
            <type>pom</type>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>read-classpath</id>
            <phase>validate</phase>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>

        </executions>
        <configuration>
          <scripts>
            <script><![CDATA[
                    def file = new File(project.properties.cpfile)
                    /* create a new property named 'cp'*/
                    project.properties.cp = file.getText()
                    println '<<< Retrieving classpath into new property named <cp> >>>'
                    println 'cp = ' + project.properties.cp
                  ]]></script>
          </scripts>
        </configuration>
      </plugin>