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

发布于 2024-09-13 09:58:37 字数 351 浏览 9 评论 0原文

我正在使用 maven-compile 插件来编译类。现在我想将一个 jar 文件添加到当前的类路径中。该文件保留在另一个位置(假设 c:/jars/abc.jar 。我更喜欢将此文件留在这里)。我怎样才能做到这一点?

如果我在参数中使用类路径:

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

它将不起作用,因为它将覆盖当前的类路径(包括所有依赖项)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吻安 2024-09-20 09:58:37

以前可能有人问过这个问题。请参阅 我可以将 jar 添加到maven 2 构建类路径而不安装它们?

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

另请参见 http://maven.apache.org/guides/简介/依赖机制介绍.html

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

长伴 2024-09-20 09:58:37

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

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

我使用 gmavenplus-plugin 来读取路径并创建属性“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>

The classpath setting of the compiler plugin are two args. Changed it like this and it worked for me:

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

I used the gmavenplus-plugin to read the path and create the property '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>
格子衫的從容 2024-09-20 09:58:37

来自 文档example 目前尚不清楚是否允许类路径操作。

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

但请参阅Java 文档(也https://www.cis.upenn.edu /~bcpierce/courses/629/jdkdocs/tooldocs/solaris/javac.html

-classpath path 指定 javac 用于查找运行 javac 所需的类或被您所在的其他类引用的类的路径
编译。覆盖默认或 CLASSPATH 环境变量
如果已设置。

也许可以获取当前的类路径并扩展它,
参见在maven中,如何输出正在使用的类路径?

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

阅读文件(将文件读入 Maven 属性

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

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>
柒七 2024-09-20 09:58:37

我们混合了此处找到的两个答案来解决类似的问题。我们的项目仅在编译阶段需要 JAR,但使用 system 范围添加本地依赖项,这是没有用的,因为 Maven 拒绝工件发布,并出现与缺少依赖项相关的错误。

使用的片段如下:

  <properties>
    <classpathfile>${basedir}/classpathfile.classpath</classpathfile>
  </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>${classpathfile}</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <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.classpathfile)
                project.properties.originalClassPath = file.getText()
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <compilerArgs>
            <arg>-cp</arg>
            <arg>${originalClassPath}${path.separator}${basedir}/../../../bin/POM_RUNTIME_PLACEHOLDER/ExtraJar.jar</arg>
          </compilerArgs>
        </configuration>
      </plugin>

Maven 能够编译并成功部署工件。
如果有人感兴趣,完整的 POM 可以在 GitHub 项目 NuReflector 下找到,defaultPOM.template< /em> 在 src/NuReflector 下。

We mixed two of the answers found here to solve a similar problem. Our project needs a JAR only in compile stage, but add a local dependency, using system scope, it is unuseful because Maven refuse the artifact publication with an error related to a missing dependency.

The snippets used are the following:

  <properties>
    <classpathfile>${basedir}/classpathfile.classpath</classpathfile>
  </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>${classpathfile}</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <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.classpathfile)
                project.properties.originalClassPath = file.getText()
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <compilerArgs>
            <arg>-cp</arg>
            <arg>${originalClassPath}${path.separator}${basedir}/../../../bin/POM_RUNTIME_PLACEHOLDER/ExtraJar.jar</arg>
          </compilerArgs>
        </configuration>
      </plugin>

Maven is able to compile and successfully deploy the artifacts.
If anyone is interested the full POM is available in GitHub under project NuReflector, defaultPOM.template under src/NuReflector.

失与倦" 2024-09-20 09:58:37

使用 antrun 插件的解决方案(将 your_jar_or_classes_folder 添加到类路径):

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>read classpath</id>
                <phase>generate-resources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <exportAntProperties>true</exportAntProperties>
                    <target>
                        <property name="cp" refid="maven.compile.classpath"/>
                    </target>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <compilerArgs>
                <arg>-cp</arg>
                <arg>${cp}${path.separator}your_jar_or_classes_folder</arg>
            </compilerArgs>
        </configuration>
    </plugin>

Solution with antrun plugin (adds your_jar_or_classes_folder to classpath):

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>read classpath</id>
                <phase>generate-resources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <exportAntProperties>true</exportAntProperties>
                    <target>
                        <property name="cp" refid="maven.compile.classpath"/>
                    </target>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <compilerArgs>
                <arg>-cp</arg>
                <arg>${cp}${path.separator}your_jar_or_classes_folder</arg>
            </compilerArgs>
        </configuration>
    </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文