java之Maven安装和配置环境变量,创建Maven项目

视频:https://www.bilibili.com/video/BV12a411w7Ah?p=7
下载: https://maven.apache.org/download.cgi
zip文件解压,放到没有中文和空格的目录中。
新建环境变量MAVEN_HOME:E:SolftWareapache-maven-3.8.1in
编辑path变量,新增%MAVEN_HOME%
cmd进入maven目录的bin中,运行命令 mvn -v,出现版本信息,成功。
安装maven要先安装JDK。

maven命令
清理:clean,清理项目生成的临时文件,一般是target目录下的文件
编译:compile,编译完成后,在src下面会生成target目录,就是编译好的源码
打包:package,java项目打包成jar,web项目打包成war,在target目录下
install,将打包的jar/war文件复制到本地仓库中,供其他模块使用
tomcat:run,在tomcat容器中运行web应用
jetty:run,调用jetty插件的run,在Jetty Servlet容器中启动web应用
部署:deploy,
-D,传入属性参数:-Dmaven.test.skip=true,跳过单元测试,deploy-Dmaven.test.skip=true就是部署的时候跳过单元测试。
-P,使用指定的Profile配置,比如项目有多个环境,开发、测试、预发、正式,在pom.xml中配置如下:

<profiles>
        <profile>
            <!-- id要和resources中的目录名字一致 -->
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <!-- 未指定环境时,默认打包dev环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <env>pro</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
    </profiles>

 还需要在build中配置,pom.xml中的build中加入:

<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>

 ${env}来获取参数,比如命令clean compile package -Ppro -Dmaven.test.skip=true,-Ppro的意思就是选择id为pro的profile配置的<env>,得到pro,那么打包的时候就选择src/main/resources/pro下面的配置文件。

配置jetty或Tomcat服务插件
jetty服务启动:jetty:run,加端口的命令:jetty:run -Djetty.port=8899,需要注释掉配置文件中的端口配置
Tomcat服务启动:Tomcat7:run,在pom.xml中配置Tomcat7,这里我使用的是IDEA自带的maven3,下载最新的maven使用不了,不知道咋回事,IDEA的版本是2019-04。
pom.xml 在 <build><plugins>中配置

<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>9090</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
          <server>tomcat7</server>
        </configuration>
      </plugin>

 pluginManagement配置节没啥用,删掉。
初始化:install,多个项目时,逐个初始化。最后要把父项目初始化,然后才能tomcat7:run
打包要建立对应的目录:在src/main/resources 下建立  dev、pro、test 目录,在三个目录下建立不同的文件
clean compile package -Ppro -Dmaven.test.skip=true,先清除之前的打包文件,再编译,再打包pro环境的,跳过测试代码

创建Maven项目
1、先设置Maven环境,File》Other Settings》Settings for New Projects》搜索maven,选中maven,Maven home directory选择安装目录,我的是E:SolftWareapache-maven-3.8.1,注意不是bin目录。
设置User Setting file,选择在安装目录下confsettings.xml,这是maven的配置文件,配置下载jar包的地址,配置maven从阿里云镜像中下载包<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
还可以配置jar包的存放地址:<localRepository>C:/Users/Administrator.MACKJON/.m2/repository</localRepository>
2、IDEA创建Mave项目,File》New》Project左侧选择Maven,右侧选择JDK1.8,勾选create from archetype,然后才能选择下面的java项目:maven-archetype-quickstart,web项目:maven-archetype-webapp
3、右下角会弹出是否启用自动导入Enable-Auto-Import,选择是。

原文地址:https://www.cnblogs.com/xsj1989/p/15033501.html