【问题标题】:Setting the a Property to what maven.compile.classpath Contains WITHOUT Ant将 a 属性设置为没有 Ant 的 maven.compile.classpath 包含的内容
【发布时间】:2010-10-20 21:44:28
【问题描述】:

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

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

非常感谢,

尼克

【问题讨论】:

    标签: java maven-2 properties classpath


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我认为不编写自己的 maven 插件就没有办法做到这一点。也就是说,您可以使用dependency:build-classpath 访问类路径。有用吗?

      【讨论】:

        【解决方案3】:

        这就是它的工作原理:

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

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

        【讨论】:

        • 我非常喜欢这个解决方案,但是因为没有antrun m2e连接器我无法使用它。
        • 这似乎不适用于子 pom 使用的父 pom 声明 ....
        【解决方案4】:

        我支持dependency:build-classpath 建议。它目前不会将其放入属性中,但可以很容易地对其进行修改。 (接受补丁)

        【讨论】:

        【解决方案5】:

        如果您需要将类路径生成为一个简单的 jar 列表(没有完整路径),您可以实现一个类似于下面示例中的插件。我需要使用“类路径”以外的属性在清单中添加类路径,因为我使用的是 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: .
        

        所以,我定义了一个这样的 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());
                }
        
            }
        

        使用此插件,您可以定义一个包含类路径的 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>
        

        并在任何你想要的地方使用这个属性,例如创建你的自定义 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>
        

        【讨论】:

          猜你喜欢
          • 2016-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-10
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多