java 将属性设置为 maven.compile.classpath 不包含 Ant 的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/773855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:42:28  来源:igfitidea点击:

Setting the a Property to what maven.compile.classpath Contains WITHOUT Ant

javamaven-2propertiesclasspath

提问by

I'd like to set a property in my pom to a classpath containing all the project's dependencies. The ant plugin does something like this, so I know it's definitely possible.

我想将 pom 中的属性设置为包含项目所有依赖项的类路径。ant 插件会做这样的事情,所以我知道这绝对是可能的。

I basically want to use ${maven.compile.classpath} wherever I like in my pom and have it 'just work'. I don't mind using plugins or anything else to achieve this.

我基本上想在我的 pom 中任何我喜欢的地方使用 ${maven.compile.classpath} 并让它“正常工作”。我不介意使用插件或其他任何东西来实现这一点。

Many thanks,

非常感谢,

Nick

缺口

采纳答案by Dominic Mitchell

I don't think that there's a way of doing this without writing your own maven plugin. That said, you can get at the classpath using dependency:build-classpath. Is that of use?

我不认为有一种方法可以在不编写自己的 maven 插件的情况下做到这一点。也就是说,您可以使用dependency:build-classpath 获取类路径。那个有用吗?

回答by yegor256

This is how it works:

这是它的工作原理:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <id>define-classpath</id>
      <phase>process-resources</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <exportAntProperties>true</exportAntProperties>
        <target>
          <property name="maven.classpath" refid="maven.runtime.classpath"/>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

After it's executed you can use ${maven.classpath}property.

执行后,您可以使用${maven.classpath}属性。

回答by Ring

Since version 2.7 the maven-dependency-plugin can now set a property to the classpath. Here's an example:

从 2.7 版开始,maven-dependency-plugin 现在可以为类路径设置一个属性。下面是一个例子:

  <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.8</version>
      <executions>
          <execution>
              <phase>generate-sources</phase>
              <goals>
                  <goal>build-classpath</goal>
              </goals>
              <configuration>
                <outputProperty>maven.compile.classpath</outputProperty>
                <pathSeparator>;</pathSeparator>
              </configuration>
          </execution>
      </executions>
  </plugin>

If you want Eclipse support here's my update site:

如果您需要 Eclipse 支持,请访问我的更新站点:

http://terraframe.github.io/m2e-maven-dependency-plugin/snapshots/

http://terraframe.github.io/m2e-maven-dependency-plugin/snapshots/

回答by Brian Fox

I second the dependency:build-classpath suggestion. It won't put it into a property currently but could easily be modified to do so. (patches accepted)

我支持dependency:build-classpath 建议。它目前不会将其放入属性中,但可以轻松修改以执行此操作。(接受补丁)

回答by Claudio Tasso

If you need to generate the classpath as a simple list of jars (without the full path), you can implement a plugin like the one in the example below. My need is to add the classpath in the Manifest using a property other than "Class-Path" because I'm using a tool like Eclipse "JarRsrcLoader" (it's similar to One-JAR) and I want to create a Manifest.MF like this:

如果您需要将类路径生成为一个简单的 jar 列表(没有完整路径),您可以实现一个类似于下面示例中的插件。我需要使用“Class-Path”以外的属性在 Manifest 中添加类路径,因为我使用的是像 Eclipse“JarRsrcLoader”这样的工具(它类似于One-JAR),我想创建一个 Manifest.MF 像这:

Manifest-Version: 1.0
Rsrc-Class-Path: ./ ssm-core-0.0.1-SNAPSHOT.jar commons-codec-1.9.jar 
 commons-io-2.4.jar ehcache-2.8.3.jar spring-beans-4.0.5.RELEASE.jar s
 sm-standalone-cryptlayer-0.0.1-SNAPSHOT.jar shiro-core-1.2.3.jar comm
 ons-beanutils-1.8.3.jar bcprov-jdk15on-1.50.jar javacsv-2.0.jar ssm-f
 ile-persistence-0.0.1-SNAPSHOT.jar spring-context-4.0.5.RELEASE.jar s
 pring-aop-4.0.5.RELEASE.jar aopalliance-1.0.jar spring-core-4.0.5.REL
 EASE.jar commons-logging-1.1.3.jar spring-expression-4.0.5.RELEASE.ja
 r slf4j-log4j12-1.7.7.jar slf4j-api-1.7.7.jar log4j-1.2.17.jar
Built-By: ctasso
Build-Jdk: 1.7.0_10
Class-Path: .

So, I defined a Maven plugin like this:

所以,我定义了一个这样的 Maven 插件:

public void execute() throws MojoExecutionException, MojoFailureException {
        try {


            MavenArchiver mavenArchiver = new MavenArchiver();

            ManifestConfiguration config = new ManifestConfiguration();
            config.setAddClasspath(true);
            Manifest manifest = mavenArchiver.getManifest(project, config);


            String classPath = manifest.getMainAttributes().getValue("Class-Path");

            getLog().debug(String.format("Setting the classpath property %s to %s",classpathVarName,classPath));

            project.getProperties().put(classpathVarName, classPath);

        } catch (DependencyResolutionRequiredException e) {
            throw new MojoFailureException(e.getMessage());
        } catch (ManifestException e) {
            throw new MojoFailureException(e.getMessage());
        }

    }

Using this plugin, you can define a property which contains the list of jars of the classpath:

使用此插件,您可以定义一个属性,其中包含类路径的 jar 列表:

<plugin>
    <groupId>it.cineca.plugins</groupId>
    <artifactId>classpath-maven-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <executions>
        <execution>
            <id>set-classpath</id>
            <phase>package</phase>
            <goals>
                <goal>setcp</goal>
            </goals>
            <configuration>
                <classpathVarName>cineca.classpath</classpathVarName>
            </configuration>
        </execution>
    </executions>   
</plugin>

and use this property wherever you want, for example for creating your custom Manifest.MF:

并在任何地方使用此属性,例如创建自定义 Manifest.MF:

<archive>
     <manifestEntries>
        <Rsrc-Class-Path>./ ${cineca.classpath}</Rsrc-Class-Path>
        <Class-Path>.</Class-Path>
        <Rsrc-Main-Class>it.cineca.cpd.starter.TestStarter</Rsrc-Main-Class>
        <Main-Class>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</Main-Class>
     </manifestEntries>
</archive>