Java入门系列16 -- Maven

2021-12-17  本文已影响0人  YanZi_33

Maven

项目构建工具
Maven的四大特性
Mac下Maven的安装与配置
Snip20211214_18.png
#配置Maven环境变量
export Maven_Home=/usr/local/apache-maven-3.8.4
export Java_Home=/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home
export PATH=$PATH:$Maven_Home/bin:$Java_Home/bin
image.png
Maven的命令
IDEA集成Maven环境
<!-- 更换阿里镜像,加快依赖下载速度 -->
<mirror>
  <id>nexus-aliyun</id>
  <mirrorOf>central</mirrorOf>
  <name>Nexus aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
image.png image.png
IDEA利用Maven创建Java项目
image.png image.png image.png image.png image.png Snip20211215_29.png image.png image.png image.png
IDEA利用Maven创建Web项目
Snip20211215_35.png Snip20211215_36.png Snip20211215_37.png
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sf</groupId>
  <artifactId>maven03</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>maven03 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <!-- JDK版本设置 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!-- 单元测试版本设置 -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!-- 引入jetty与tomcat的maven插件 -->
  <build>
    <finalName>maven03</finalName>
    <plugins>
      <plugin>
        <!-- jetty插件 -->
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.25</version>
        <configuration>
          <!-- 热部署 每10秒扫描一次 -->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 可指定当前项目的站点名 -->
          <contextPath>/maven03</contextPath>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>9090</port> <!-- 设置启动端口号 -->
            </connector>
          </connectors>
        </configuration>
      </plugin>

      <!-- tomcat插件 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8080</port> <!-- 启动端口 默认8080 -->
          <path>/test</path> <!-- 项目站点名 对外访问路径 -->
          <uriEncoding>UTF-8</uriEncoding> <!-- 字符集 -->
          <server>tomcat7</server> <!-- 服务器名称 -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
image.png Snip20211215_38.png Snip20211215_40.png Snip20211215_39.png Snip20211215_41.png
Maven仓库
<!-- 本地仓库地址: 存放jar包 -->
<localRepository>自定义路径</localRepository>
Maven环境下构建多模块项目
Snip20211215_43.png Snip20211215_45.png Snip20211215_46.png Snip20211215_48.png Snip20211215_49.png Snip20211215_50.png Snip20211215_52.png image.png image.png
<groupId>com.sf</groupId>
<artifactId>maven_dao</artifactId>
<version>1.0-SNAPSHOT</version>
image.png image.png
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sf</groupId>
  <artifactId>maven_controller</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>maven_controller Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>

    <!-- 引入maven_service层 -->
    <dependency>
      <groupId>com.sf</groupId>
      <artifactId>maven_service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!-- 引入Servlet依赖 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>maven_controller</finalName>

    <!-- 添加插件 -->
    <plugins>
      <!-- tomcat插件 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8080</port> <!-- 启动端口 默认8080 -->
          <path>/web</path> <!-- 项目站点名 对外访问路径 -->
          <uriEncoding>UTF-8</uriEncoding> <!-- 字符集 -->
          <server>tomcat7</server> <!-- 服务器名称 -->
        </configuration>
      </plugin>
    </plugins>

  </build>
</project>
image.png image.png
package com.sf.dao;

public class UserDao {

    public static void TestDao() {
        System.out.println("UserDao Test");
    }
}
package com.sf.service;

import com.sf.dao.UserDao;

public class UserService {

    public static void testService() {
        System.out.println("UserService Test");
        //调用maven_dao层的方法
        UserDao.TestDao();
    }
}
package com.sf.controller;

import com.sf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet service");
        //调用maven_service层的方法
        UserService.testService();
    }
}
image.png
Maven的打包
name=root
password=root
<bean>
    dev
</bean>
name=test
password=test
<bean>
    test
</bean>
name=product
password=product
<bean>
    product
</bean>
  <!-- 打包配置 -->
  <profiles>
    <profile>
      <id>id</id>
      <properties>
        <env>dev</env>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
      </properties>
    </profile>

    <profile>
      <id>test</id>
      <properties>
        <env>test</env>
      </properties>
    </profile>

    <profile>
      <id>product</id>
      <properties>
        <env>product</env>
      </properties>
    </profile>

  </profiles>
    <!-- 资源文件的配置 -->
    <resources>
      <resource>
        <directory>src/main/resources/${env}</directory>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sf</groupId>
  <artifactId>maven03</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>maven03 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <!-- JDK版本设置 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!-- 打包配置 -->
  <profiles>
    <profile>
      <id>id</id>
      <properties>
        <env>dev</env>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
      </properties>
    </profile>

    <profile>
      <id>test</id>
      <properties>
        <env>test</env>
      </properties>
    </profile>

    <profile>
      <id>product</id>
      <properties>
        <env>product</env>
      </properties>
    </profile>

  </profiles>

  <!-- 单元测试版本设置 -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!-- 引入jetty与tomcat的maven插件 -->
  <build>
    <finalName>maven03</finalName>
    <!-- 资源文件的配置 -->
    <resources>
      <resource>
        <directory>src/main/resources/${env}</directory>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <!-- jetty插件 -->
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.25</version>
        <configuration>
          <!-- 热部署 每10秒扫描一次 -->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 可指定当前项目的站点名 -->
          <contextPath>/maven03</contextPath>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>9090</port> <!-- 设置启动端口号 -->
            </connector>
          </connectors>
        </configuration>
      </plugin>

      <!-- tomcat插件 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8080</port> <!-- 启动端口 默认8080 -->
          <path>/test</path> <!-- 项目站点名 对外访问路径 -->
          <uriEncoding>UTF-8</uriEncoding> <!-- 字符集 -->
          <server>tomcat7</server> <!-- 服务器名称 -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
image.png
Maven的依赖
上一篇 下一篇

猜你喜欢

热点阅读