سؤال

كيف يمكنني تكوين المترجم maven لاستخدام Java 5 لرمز الاختبار الخاص بي وjava 1.4 للكود الرئيسي الخاص بي؟

هل كانت مفيدة؟

المحلول

إذا كنت تريد ضبط التوافق على إصدار Java ذي الصلة، فيمكنك تكوين البرنامج الإضافي للمترجم لكل عملية تنفيذ.بافتراض أن Maven يستخدم JDK على الأقل بنفس الإصدار الأعلى الذي تحدده.باستخدام الخصائص، يمكنك تجاوز هذا التكوين في سطر الأوامر أو في طفل إذا لزم الأمر:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>${compileSource}</source>
    <target>${compileSource}</target>
  </configuration>
  <executions>
    <execution>
      <id>test-compile</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>testCompile</goal>
      </goals>
      <configuration>
        <source>${testCompileSource}</source>
        <target>${testCompileSource}</target>
      </configuration>
    </execution>
  </executions>
</plugin>
...
<properties>
  <compileSource>1.4</compileSource>
  <testCompileSource>1.5</testCompileSource>
</properties>

إذا كنت تقصد استخدام مختلف المترجمين, ، هذا أكثر تعقيدًا بعض الشيء.كما تحتاج إلى تحديد المسار إلى JDK وإصدار المترجم الذي تستخدمه.مرة أخرى يمكن تعريف هذه في الخصائص.على الرغم من أنك قد ترغب في تعريفها في ملف settings.xml الخاص بك

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>${compileSource}</source>
    <target>${compileSource}</target>
    <executable>${compileJdkPath}/bin/javac</executable>
    <compilerVersion>${compileSource}</compilerVersion>
  </configuration>
  <executions>
    <execution>
      <id>test-compile</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>testCompile</goal>
      </goals>
      <configuration>
        <source>${testCompileSource}</source>
        <target>${testCompileSource}</target>
        <executable>${testCompileJdkPath}/bin/javac</executable>
        <compilerVersion>${testCompileSource}</compilerVersion>
      </configuration>
    </execution>
  </executions>
</plugin>
...
<properties>
  <compileSource>1.4</compileSource>
  <testCompileSource>1.5</testCompileSource>
  <compileJdkPath>path/to/jdk</compileJdkPath>
  <testCompileJdkPath>path/to/test/jdk<testCompileJdkPath>
</properties>

لاحظ أنه قد يكون من المنطقي تحديد تكوينات المترجم في ملفات التعريف، واحدة لكل JDK تدعمها، بحيث لا تعتمد بنياتك العادية على الخصائص التي يتم تعيينها.

أيضًا، في Maven 3.x، تحتاج إلى تضمين ملف fork المعلمة عند تحديد الملف القابل للتنفيذ، على سبيل المثال:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
      <execution>
        <id>default-testCompile</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
        <configuration>
          <fork>true</fork>
          <executable>${testCompileJdkPath}/bin/javac</executable>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>            
      </execution>
    </executions>
  </plugin>

نصائح أخرى

ولم يكن لدي أي حظ مع الإجابة مقبولة تجميع جافا 7 المصدر وجافا 8 مصادر الاختبار باستخدام maven-compiler-plugin، الإصدار 3.5.1. لأن البرنامج المساعد الترجمة استخدام المعلمة المصدر / الهدف على حد سواء، المصادر الرئيسية والاختبار.

ولكن تبين لي أن هناك معلمات التكوين منفصلة عن مصدر الاختبار والهدف.

وهكذا بالنسبة لي الحل الذي عملت و

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <testSource>1.8</testSource>
                <testTarget>1.8</testTarget>
            </configuration>
        </plugin>
    </plugins>
</build>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top