如何在使用不同Java版本的POM中有一个额外的编译目标? [英] How to have an extra compile goal in POM, which uses a different Java version?

查看:108
本文介绍了如何在使用不同Java版本的POM中有一个额外的编译目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个,该库通过内联标签使用.它打算供其他库使用,以将示例代码插入其JavaDoc.

I have a library which is used via inline taglets. It's intended to be used by other libraries, for inserting example code into their JavaDoc.

该库已完成,并且 hallelujah 到昨天为止,该库现在位于我现在是第一次将这个库实现到另一个项目中.该项目使用java.version 1.5编译其代码.但是标签库需要1.7.不仅用于运行javadoc.exe,而且还用于编译可选的"taglet customr"类.

I'm now implementing this library into another project for the first time. That project compiles its code with java.version 1.5. But the taglet library requires 1.7. Not just for running javadoc.exe, but also for compiling optional "taglet customizer" classes.

UPDATE :这些定制器类是由每个开发人员创建的,即使用库中的taglet库的人员.定制程序类(完全可选),只有 ="nofollow">高级功能,并且您可以不创建任何高级功能而已).

UPDATE: These customizer classes are created by each developer--the person using the taglet library in their library. The customizer classes (which are completely optional--they're needed only for advanced features, and you can get away with creating none) are required to be compiled before executing javadoc.exe.

因此,它需要实现以下目标:

So it needs the following goals:

  1. 编译主库类(需要Java 1.5)(mvn compile)
  2. 编译定制器(需要Java 1.7)(mvn compilecodelet?)
  3. 运行javadoc.exe(Java 1.7)(`mvn docs'?)
  1. Compile the main library classes (requires Java 1.5) (mvn compile)
  2. Compile the customizers (requires Java 1.7) (mvn compilecodelet?)
  3. Run javadoc.exe (Java 1.7) (`mvn docs'?)

因此,

mvn install会依次调用这三个目标.

mvn install would therefore call these three goals in order.

我是Maven的新手,并且希望能获得一些有关如何执行此操作的建议.到目前为止,这是我发现的:

I'm new to Maven, and would appreciate some advice on how to do this. This is what I've found out so far:

这是项目当前的compile目标:

<properties>
    <java.version>1.5</java.version>
</properties>

<plugin>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.3.2</version>
   <configuration>
      <encoding>UTF-8</encoding>
      <source>${java.version}</source>
      <target>${java.version}</target>
   </configuration>
</plugin>

似乎应该将此项目的JDK升级到1.7,并使用

It seems the JDK for this project should be upgraded to 1.7, with the main classes compiled with the <source> and <target> flags set to 1.5, and the taglet-customizers compiled with those flags set to 1.7.

一些参考文献:

  • maven-compiler-plugin overview
  • Compiling Sources Using A Different JDK, but I believe the only JDK hi

如何设置这个额外的compilecodelet目标,并设置整个项目,使其可以处理两个JDK版本?这样,mvn install(以及其他任何主要"目标)也可以按适当的顺序称为这个新的子目标吗?

How can I set up this extra compilecodelet goal, and set the whole project up so it can handle both JDK versions? And so that mvn install (and whatever other "master" goals) also call this new sub-goal in the proper sequence?

正如我所说,我是Maven的新手,虽然我开始了解点点滴滴,但我还不知道如何将所有这些放在一起.

As I said, I'm new to Maven, and although I'm starting to understand bits and pieces, but I don't know how to put this all together yet.

感谢您的帮助.

推荐答案

我分享了其他对您的问题发表评论的人的疑虑,对我来说,做您的事情似乎冒险并且容易出错.最好将Codelet扩展类放在一个单独的项目中,并使用source&目标1.7,并在库POM的javadoc插件配置中添加对小码扩展jar的依赖关系.

I share the misgivings of the others who commented on your question, doing what you ask seems risky and error-prone to me. It might be better to put the codelet extension classes in a separate project, build them with source & target of 1.7, and add a dependency on the codelet extension jar in the library POM's javadoc plugin configuration.

但是,如果不可能的话,我会尝试这样的事情.它未经测试,但是应该可以给您带来启发.

However if that is not possible, I would try something like this. It is untested but should give you the idea.

假设此目录结构为:

basedir
  src
    main
      java
         regularLibCodePackage
         codeletExtensionPackage

<properties>
  <java.version>1.5</java.version>
  <!-- sets encoding for the whole Maven build -->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.3.2</version>  <!--consider using latest plugin version -->
  <configuration>
    <!-- applies to all executions unless overridden by an execution -->
    <source>${java.version}</source>
    <target>${java.version}</target>
  </configuration>
  <executions>
    <execution>
      <id>default-compile</id>
      <configuration>
        <excludes>
          <!-- Not sure exactly what the path should be here, the docs aren't 
               clear. Maybe it should be an absolute path, e.g. 
               ${project.basedir}/src/main/java/codeletExtensionPackage?
               When you figure it out let me know and I'll edit the response. -->
          <exclude>codeletExtensionPackage</exclude>
        </excludes>
      </configuration>
    </execution>
    <execution>
      <id>codelet-compile</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <excludes>
          <!-- See note above about what to put here -->
          <exclude>regularLibCodePackage</exclude>
        </excludes>
      </configuration>
    </execution>
  </executions>
</plugin>

您是否需要将1.7个类排除在最终的jar之外?换句话说,运行Javadoc插件仅需要其他类吗?如果答案是是",那么您还需要调整默认的jar插件执行. (如果答案是肯定的,这是将小码类放在单独项目中的另一个原因!)

Do you need to keep the 1.7 classes out of the final jar? In other words, are the additional classes just required for running the Javadoc plugin? If the answer is 'yes' then you will also need to adjust the default jar plugin execution. (And if the answer is yes, this is another reason to put the codelet classes in a separate project!)

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.5</version>  <!--consider using latest plugin version -->
  <executions>
    <execution>
      <id>default-jar</id>
      <configuration>
        <excludes>
          <exclude>codeletExtensionPackage</exclude>
        </excludes>
      </configuration>
    </execution>
  </executions>
</plugin>

我在编译器和jar插件配置中使用了excludes,也有一个随附的include块.我把它留给您找出可行的配置,这应该可以帮助您入门.

I used excludes in the compiler and jar plugin configs, there is a companion includes block as well. I leave it to you to figure out the working config, this should get you started.

这篇关于如何在使用不同Java版本的POM中有一个额外的编译目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆