天天看點

Maven安裝及環境變量配置1、關于Maven2、Maven下載下傳3、Maven安裝4、其他5、關于macOS上的maven

1、關于Maven

Maven項目對象模型(POM),是可以通過一小段描述資訊來管理項目的建構、報告和文檔的軟體項目管理工具。 Maven也是一個用于Java web開發的容器,在這個容器中提供了很多Java web開發的所需的架構包和其他元件。

2、Maven下載下傳

Maven可以從Apache網站上下載下傳,下載下傳位址: http://maven.apache.org/,進入後點選左側的Download,然後在下面的下載下傳表格中下載下傳“apache-maven-*.*.*-bin.zip”。

3、Maven安裝

3.1、安裝

将以上下載下傳好的maven解壓後移動到自定的路徑中。

3.2、環境變量配置

①右鍵桌面上的計算機,選擇屬性; ②點選進階系統設定(在屬性頁的左上側); ③彈出的視窗中點選右下側的環境變量按鈕; ④在系統變量區點選建立按鈕,填寫變量名為MAVEN_HOME,變量值為自定義的Maven的目錄的環境變量,如下圖所示;

Maven安裝及環境變量配置1、關于Maven2、Maven下載下傳3、Maven安裝4、其他5、關于macOS上的maven

⑤在系統變量中找到Path環境變量,新增“;%MAVEN_HOME%\bin;”的配置;

3.3、驗證安裝是否成功

①按下Windows+R鍵運作,輸入cmd,按下Enter鍵; ②在指令行視窗中輸入“mvn -v”指令,若出現如下資訊,則代表配置成功。

Maven安裝及環境變量配置1、關于Maven2、Maven下載下傳3、Maven安裝4、其他5、關于macOS上的maven

4、其他

除了以上配置之外,需要在内部修改的配置還有以下幾點(都需要進入maven的conf目錄中打開settings.xml):

4.1、maven的本地倉庫配置

在以下配置資訊的下方配置:

<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
           

需要在以上配置資訊的下方添加如下代碼的配置資訊,其中兩個尖括号<localRepository></localRepository>之間配置自定義的本地倉庫的路徑。

<localRepository>/../..</localRepository>
           

4.2、maven網絡鏡像優化配置

為什麼要進行網絡鏡像優化配置呢?原因在于maven這個家夥是外國人開發的,但是并沒有在中國設定伺服器,在國内用大天朝的網絡,如果用它自己預設配置的伺服器的話下載下傳jar包和鏡像就要花費大半天的時間來下載下傳jar包,時間就這麼被浪費了,是以我們要解決網絡問題。 還好馬雲爸爸給力啊,阿裡給提供了阿裡自己的鏡像庫,可以從阿裡的鏡像庫進行下載下傳,時間節省了,得不償失呢! 先找到如下配置:

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors>
           

在以上配置中添加如下配置:

<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
           

以下是配置完成後的效果:

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->

    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>

  </mirrors>
           

以上阿裡鏡像中心,親測,速度快得飛起。

4.3、JDK适配配置

由于maven對JDK 8不是很友好,在配置maven項目的時候會産生警告或者錯誤,需要進行動态适配。

解決方案: 找到如下配置:

<profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>
           

在其中添加如下配置:

<profile>
      <id>jdk-1.8</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      </properties>
    </profile>
           

以下是配置好之後的效果:

<profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->

    <profile>
      <id>jdk-1.8</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      </properties>
    </profile>

  </profiles>
           

5、關于macOS上的maven

在下載下傳的時候下載下傳”apache-maven-*.*.*-bin.tar.gz“的安裝包,解壓類似,環境變量無需配置,在内容配置的時候需要進行和第4點中相類似的配置。 到此,maven的安裝和環境變量的配置的方法介紹完了,希望采納。