4

クラスを生成し、JDKのtools.jarでコンパイルするant build.xmlを使用しています。

MAVEN 2.2.1 バージョンを使用しています。MAVEN を実行するための JDK 1.5。Maven 2.2.1 バージョンは JDK 1.5 以降のみをサポートするため、それを使用する必要があります。

この maven-antrun-plugin では、コンパイラのソース バージョンもターゲット バージョンも指定できません。そのため、生成されたクラスは、現在実行中の JDK である JVM に対してコンパイルされ、その rt.jar およびプラグインの依存関係で提供される (または jvm の lib/ext ディレクトリに配置される) tools.jar を使用します。

Maven は 1.5 JVM (jdk1.5.0_22) で実行されるため、これらのクラスを JDK 1.4.2 バージョンでコンパイルするには、これらのクラスをデプロイするサーバーが 1.4 JVM で実行されているため、例外が発生します。私はEclipseまたはコマンドラインからMAVENプラグインでコンパイルしています。

別のJavaバージョンを使用してクラスをコンパイルするようにantrunに指示する方法が見つかりませんでした。次の回避策を試しました:

  • tools.jar の依存関係を 1.4 バージョンを指すように変更します => コンパイラは現在実行中の 1.5 JVM の rt.jar を使用するため、クラス ファイルのバージョンが一致しません (バージョン 49.0、48.0 を期待)
  • 依存関係を 1.4 rt.jar に追加します => rt.jar はブート クラスパスで指定する必要があるため、何も変更されません。

以下は、私が使用しているサンプルコードです。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>install</id>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerVersion>${java-version}</compilerVersion>
                <compilerArguments>
                    <classpath>${java.home}/lib/tools.jar</classpath>
                    <classpath>${java.home}/jre/lib/rt.jar</classpath>
                </compilerArguments>
                <tasks>
                    <ant antfile="WPSEjb_build.xml"/>
                </tasks>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>crimson</groupId>
            <artifactId>crimson</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>sun.jdk</groupId>
            <artifactId>tools</artifactId>
            <version>1.4.2</version>
            <scope>system</scope>
            <systemPath>${java.home}/lib/tools.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>rt</artifactId>
            <version>${java-version}</version>
            <scope>system</scope>
            <systemPath>${java.home}/jre/lib/rt.jar</systemPath>
        </dependency>
    </dependencies>
</plugin>

このような例外が発生しています。

WPSClient.java:22: cannot access java.lang.Object
 [wlwBuild] [Build] bad class file: C:\Java\jdk1.5.0_22\jre\lib\rt.jar(java/lang/Object.class)
 [wlwBuild] [Build] class file has wrong version 49.0, should be 48.0
 [wlwBuild] [Build] Please remove or make sure it appears in the correct subdirectory of the classpath.
 [wlwBuild] [Build] public static WPSServerRemote getWPSServer() throws MitchellException {
 [wlwBuild] [Build] ^
 [wlwBuild] [Build] 3 errors
 [wlwBuild] [Build] BUILD FAILED
 [wlwBuild] [Build] Compile failed; see the compiler error output for details.
 [wlwBuild] [Build] 
 [wlwBuild] java.lang.reflect.InvocationTargetException
 [wlwBuild] java.lang.reflect.InvocationTargetException
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [wlwBuild]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [wlwBuild]     at java.lang.reflect.Method.invoke(Method.java:592)
 [wlwBuild]     at workshop.core.Compile.start(Compile.java:19)
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [wlwBuild]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [wlwBuild]     at java.lang.reflect.Method.invoke(Method.java:592)
 [wlwBuild]     at workshop.core.Starter.invokeStart(Starter.java:34)
 [wlwBuild]     at workshop.core.Compile.main(Compile.java:9)
 [wlwBuild] Caused by: java.lang.NoClassDefFoundError: org/apache/crimson/tree/XmlDocument
 [wlwBuild]     at workshop.util.ide.PreferencesNode._export(PreferencesNode.java:540)
 [wlwBuild]     at workshop.util.ide.PreferencesNode.exportSubtree(PreferencesNode.java:820)
 [wlwBuild]     at workshop.util.ide.PreferencesNode.flush(PreferencesNode.java:984)
 [wlwBuild]     at workshop.core.App$15.run(App.java:1000)
 [wlwBuild]     at workshop.core.asynctask.AsyncTaskManager.showDialogWhileRunning(AsyncTaskManager.java:272)
 [wlwBuild]     at workshop.core.asynctask.AsyncTaskManager.showDialogWhileRunning(AsyncTaskManager.java:482)
 [wlwBuild]     at workshop.core.App.exit(App.java:994)
 [wlwBuild]     at workshop.core.CompileHelper.compile(CompileHelper.java:298)

maven-antrun-plugin を使用して、クラスをコンパイルするように JDK 1.4.2 バージョンをセットアップする方法を教えてください。

4

2 に答える 2

0

新しいバージョンの JDK は、古い JDK で実行されるコードをコンパイルできます。サーバー上のJDKのバージョンが1.4の場合は、ターゲットを1.4に設定してください。

コンパイラがこれをどのように処理するかをよりよく理解するために、1.5のJava コンパイラの使用ガイドを確認し、ターゲット オプションを確認することをお勧めします。

編集: 明確にするために、ターゲット バージョンを呼び出す pom の部分は ${java-version} です。これは、ターゲット オプションとして javac に渡されます。マイナー バージョン番号が使用されていないため、1.4 と指定する必要があります。

于 2011-08-11T00:35:12.843 に答える
-2

次のコマンドを使用してビルドできます。

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
              <execution>
                <id>install</id>
                <phase>install</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
              </execution>
            </executions>
            <configuration>             
              <executable>C:\test\Ejb.bat</executable>
            </configuration>
          </plugin>

ant -f build.xmlファイルの下でビルドコマンドを呼び出していEjb.batます。私が提供しているすべての構成を使用しているbuild.xmlため、JDK 1.4.2バージョンでビルドできます。

于 2011-08-11T18:08:51.847 に答える