Maven教程(Eclipse配置及maven專案)

哈哈哈hh發表於2022-07-13

映象下載、域名解析、時間同步請點選  阿里雲開源映象站

Eclipse中配置maven

1.Eclipse中預設的Maven配置

file

可以使用預設的,本地倉庫在當前使用者下的.m2資料夾下。

2.配置我們自己安裝的maven

2.1指定配置安裝maven的路徑

file

file

2.2關聯setting.xml檔案

file

2.3配置setting.xml

中央倉庫的地址在國外直接下載jar會很慢,所以我們需要通過代理的方式下載

<!-- 阿里代理映象地址 -->
<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>
  <mirrorOf>*</mirrorOf>
</mirror>

file

3.建立Maven專案

file

3.1建立java工程

建立步驟

file

file

file

然後等待…

建立好的專案結構

file

此處報錯的原因是jdk版本問題,我們使用的maven的3.6.0jdk必須是1.7+當前使用的是1.5.所以我們需要修改jdk的版本,解決方式有兩種。

a.第一種解決方式

第一種解決方法是在pom.xml檔案中新增如下程式碼

<project xmlns="
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dpb</groupId>
  <artifactId>MavenDemo01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <build>
    <plugins>
      <!-- 設定編譯環境 1.8 -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

然後如下操作即可:

file

但這種方式有個不太好的地方是每次建立專案都需要新增這個程式碼,第二種方式比較好解決。

b.第二種解決方式

在setting.xml配置檔案中新增設定

<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節點中新增註意

然後在eclipse中update一下就可以了

file

在update下專案就可以了

file

3.2建立Web工程

建立步驟:

file

file

file

然後等待…

建立好的專案結構

file

解決報錯

報錯原因:

缺少web.xml檔案

file

解決方法:

1.手動建立WEB-INF\web.xml檔案

2.選中專案右鍵properties選單

file

右擊maven專案,找到ProjectFacets 取消選中 Dynamic Web Module選項,點選應用,再選中Dyanmic Web Module會出現一個選項卡

file

點選彈出的選項卡後

file

輸入src/main/webapp點選OK

file

這時在去檢視我們的本地倉庫會發現多了很多東西

file

這是從中央倉庫下載下來的jar包

讓專案跑起來

1.新增個靜態頁面

file

2.通過maven將專案打成war包

file

file

file

將此war包部署到Tomcat中即可

Tomcat外掛使用

打成war包手動部署這種方式在開發過程中不是很高效,這時我們可以在專案中整合Tomcat外掛來快速部署執行

1.在pom.xml檔案中新增

<project xmlns="
  xsi:schemaLocation="
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dpb</groupId>
  <artifactId>MavenDemo01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <!-- 因為是web專案所以需要servlet -->
    <!-- 
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <!-- tomcat外掛 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <!-- 埠號 -->
          <port>8082</port>
          <!-- /表示訪問路徑 省略專案名 -->
          <path>/</path>
          <!-- 設定編碼方式 -->
          <uriEncoding>utf-8</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

file

2.執行

file

file

輸入: tomcat7:run 然後執行

第一次要下載一些資源會比較慢。

file

file

本文轉自:https://blog.51cto.com/u_15494758/5432885


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70003733/viewspace-2905535/,如需轉載,請註明出處,否則將追究法律責任。

相關文章