您的位置:

Maven JDK 配置指南

一、Maven 简介

Maven 是一个Java项目的自动化构建工具,通过Maven进行构建,可以使构建过程更加稳定、可靠。它可以自动下载需要的依赖和插件,自动识别出项目中需要编译、打包和测试的源代码,并进行适当的管理和配置。

在使用 Maven 的过程中,我们需要将 JDK 配置好,才能完成项目的自动化构建。下面将从多个方面进行 JDK 的配置。

二、配置 JDK 环境变量

首先,需要配置 JDK 环境变量。在 Windows 环境下,需要将 JDK 的 bin 目录路径添加到 PATH 环境变量中。

<!-- Windows 环境下,添加 JDK bin 目录到 PATH 环境变量 -->
<profiles>
  <profile>
    <id>windows</id>
    <activation>
      <os>
        <family>windows</family>
      </os>
    </activation>
    <properties>
      <path.separator>;</path.separator>
      <jdk.home>C:\Program Files\Java\jdk1.8.0_211</jdk.home>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <executions>
            <execution>
              <phase>initialize</phase>
              <goals>
                <goal>add-source</goal>
                <goal>add-resource</goal>
              </goals>
              <configuration>
                <source>${jdk.home}\src.zip</source>
                <resources>
                  <resource>
                    <directory>${jdk.home}\jre\lib\rt.jar</directory>
                    <includes>
                      <include>**/*</include>
                    </includes>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

三、配置 JAVA_HOME 环境变量

然后,需要将 JDK 的安装路径添加到 JAVA_HOME 环境变量中,以便 Maven 可以找到 JDK。

<!-- Windows 环境下,配置 JAVA_HOME 环境变量 -->
<profiles>
  <profile>
    <id>windows</id>
    <activation>
      <os>
        <family>windows</family>
      </os>
    </activation>
    <properties>
      <path.separator>;</path.separator>
      <jdk.home>C:\Program Files\Java\jdk1.8.0_211</jdk.home>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <executions>
            <execution>
              <phase>initialize</phase>
              <goals>
                <goal>add-source</goal>
                <goal>add-resource</goal>
              </goals>
              <configuration>
                <source>${jdk.home}\src.zip</source>
                <resources>
                  <resource>
                    <directory>${jdk.home}\jre\lib\rt.jar</directory>
                    <includes>
                      <include>**/*</include>
                    </includes>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <fork>true</fork>
              <compilerId>javac</compilerId>
              <verbose>true</verbose>
              <executable>${jdk.home}/bin/javac</executable>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>com.sun.tools</groupId>
                <artifactId>javac</artifactId>
                <version>9+181</version>
                <scope>system</scope>
                <systemPath>${jdk.home}/lib/tools.jar</systemPath>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
  </profile>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <jdk.home>/usr/lib/jvm/java-openjdk</jdk.home>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <executions>
            <execution>
              <phase>initialize</phase>
              <goals>
                <goal>add-source</goal>
                <goal>add-resource</goal>
              </goals>
              <configuration>
                <source>${jdk.home}/src.zip</source>
                <resources>
                  <resource>
                    <directory>${jdk.home}/jre/lib/rt.jar</directory>
                    <includes>
                      <include>**/*</include>
                    </includes>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
  </profile>
</profiles>

四、配置构建插件

为了使 Maven 正确地使用 JDK 进行编译和测试,我们需要为项目配置构建插件。

<!-- 配置 Maven 构建插件,使用 JDK 进行编译和测试 -->
<profiles>
  <profile>
    <id>windows</id>
    <activation>
      <os>
        <family>windows</family>
      </os>
    </activation>
    <properties>
      <path.separator>;</path.separator>
      <jdk.home>C:\Program Files\Java\jdk1.8.0_211</jdk.home>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>build-helper-maven-plugin</artifactId>
          <executions>
            <execution>
              <phase>initialize</phase>
              <goals>
                <goal>add-source</goal>
                <goal>add-resource</goal>
              </goals>
              <configuration>
                <source>${jdk.home}\src.zip</source>
                <resources>
                  <resource>
                    <directory>${jdk.home}\jre\lib\rt.jar</directory>
                    <includes>
                      <include>**/*</include>
                    </includes>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <fork>true</fork>
              <compilerId>javac</compilerId>
              <verbose>true</verbose>
              <executable>${jdk.home}/bin/javac</executable>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>com.sun.tools</groupId>
                <artifactId>javac</artifactId>
                <version>9+181</version>
                <scope>system</scope>
                <systemPath>${jdk.home}/lib/tools.jar</systemPath>
              </dependency>
            </dependencies>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
              <forkCount>1</forkCount>
              <argLine>-Xmx512m -XX:MaxPermSize=256m -Duser.timezone=GMT+08</argLine>
              <systemPropertyVariables>
                <java.awt.headless>true</java.awt.headless>
              </systemPropertyVariables>
              <workingDirectory>${project.basedir}</workingDirectory>
              <testFailureIgnore>true</testFailureIgnore>
            </configuration>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
  </profile>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <jdk.home>/usr/lib/jvm/java-openjdk</jdk.home>
    </properties>
    <build>
      <plugins>
        <plugin>