zoukankan      html  css  js  c++  java
  • Example For maven-compiler-plugin

    1. Compiling Sources Using A Different JDK

    The compilerVersion parameter can be used to specify the version of the compiler that the plugin will use. However, you also need to set fork to true for this to work. For example:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable><!-- path-to-javac --></executable>
              <compilerVersion>1.3</compilerVersion>
            </configuration>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>
    
    

    To avoid hard-coding a filesystem path for the executable, you can use a property. For example:

    <executable>${JAVA_1_4_HOME}/bin/javac</executable>
    

    Each developer then defines this property in settings.xml, or sets an environment variable, so that the build remains portable.

    <settings>
      [...]
      <profiles>
        [...]
        <profile>
          <id>compiler</id>
            <properties>
              <JAVA_1_4_HOME>C:Program FilesJavaj2sdk1.4.2_09</JAVA_1_4_HOME>
            </properties>
        </profile>
      </profiles>
      [...]
      <activeProfiles>
        <activeProfile>compiler</activeProfile>
      </activeProfiles>
    </settings>
    

    2. Setting the -source and -target of the Java Compiler

    Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target. The Compiler Plugin can also be configured to provide these options during compilation.

    For example, if you want to enable assertions (-source 1.4) and also want the compiled classes to be compatible with JVM 1.4 (-target 1.4), you can then put:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.4</source>
              <target>1.4</target>
            </configuration>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>
    

    Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs.

    3. Compile Using Memory Allocation Enhancements

    The Compiler Plugin accepts configurations for meminitial and maxmem. You can follow the example below to set the initial memory size to 128MB and the maximum memory usage to 512MB:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <fork>true</fork>
              <meminitial>128m</meminitial>
              <maxmem>512m</maxmem>
            </configuration>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>
    

    4. Pass Compiler Arguments

    Sometimes, you need to pass other compiler arguments that are not handled by the Compiler Plugin itself but is supported by the compilerId selected. For such arguments, use the Compiler Plugin's compilerArgs parameter The following example passes compiler arguments to the javac compiler:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <compilerArgs>
                <arg>-verbose</arg>
                <arg>-Xlint:all,-options,-path</arg>
              </compilerArgs>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>
    
  • 相关阅读:
    Python变量、标识符
    PyCharm中设置作者信息
    去中心化
    复杂逻辑---取数
    行政机构处理
    2020.11案例分析
    中间表
    软考--项目管理名词
    软考--近6次上午考点分析
    软考--学习方法、做题方法
  • 原文地址:https://www.cnblogs.com/javaDeveloper/p/5058077.html
Copyright © 2011-2022 走看看