• maven学习记录


    一、准备工作

    1、下载

    下载地址:http://maven.apache.org/download.cgi

    2、配置环境变量

    添加MAVEN_HOME:C:apache-maven-3.5.0
    PATH中加入 :%MAVEN_HOME%in

    3、测试

    mvn -v

    二、构建Java项目

    创建一个空文件夹,如:C:/java/pro2,在终端中输入命令:

    mvn -B archetype:generate -DgroupId=com.txl -DartifactId=htmlparser

    -DgroupId:project-packaging
    -DartifactId:project-name

    三、在pom.xml中添加远程仓库,如:

    <dependency>
      <!-- jsoup HTML parser library @ http://jsoup.org/ -->
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.10.2</version>
    </dependency>

    然后进入项目目录执行:

    mvn  dependency:copy-dependencies 

    windows会默认下载到:C:Documents and Settings{your-username}.m2

    修改本地仓库位置:进入maven目录下的config文件,修改settings.xml,加入配置节,然后重新执行上面的命令:

    <localRepository>C:javapro2mvnrepo</localRepository>

    maven默认会从 repo.maven.apache.org/maven2 中下载
    maven默认中央仓库地址:http://repo1.maven.org/maven2/
    搜索相关软件:http://search.maven.org/http://mvnrepository.com/

    四、编译并执行

    进入项目目录编译:

    mvn compile

    各种下载和检查后,如果没有程序错误,那么会生成一个 target 文件夹,然后执行:

    mvn exec:java -Dexec.mainClass="com.txl.App" 

    五、IDEA设置

    1、设置路径
    这里写图片描述

    2、右击项目名称,选择 Add Framework Support
    这里写图片描述

    3、选择maven,点击ok
    这里写图片描述

    4、pom.xml基本配置模板:

    <?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>groupId</groupId>
        <artifactId>pro1</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <!-- jsoup HTML parser library @ http://jsoup.org/ -->
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>1.10.2</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <executions>
                        <execution>
                            <phase>test</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>myIndex</mainClass>
                                <arguments>
                                    <argument></argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

    5、添加maven运行配置:
    这里写图片描述

    六、生命周期

    1、Clean:构建之前进行一些清理工作

    pre-clean:执行一些需要在clean之前完成的工作
    clean:移除所有上一次生成的文件
    post-clean:执行一些需要在clean之后立刻完成的工作

    2、Default:编译、测试、打包、部署等

    process-resources:复制、打包资源文件
    compile:编译项目
    test:运行测试(需要配置测试框架)
    package:打包成可发布的格式,如 jar、war
    install:将包安装至本地仓库
    deploy:将最终的包复制到远程的仓库

    3、Site:生成项目报告、站点、发布站点

    七、多项目联合构建

    方式1:jar

    在pom.xml中加入 (默认不写就是jar):

    <packaging>jar</packaging>

    进入终端执行:

    mvn package

    这时本地仓库会出现一个 jar文件,然后在其他项目中就可以直接引用该jar:

    <dependency>
        <groupId>com.txl</groupId>
        <artifactId>txl</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    事实上这种方式并不方便,因为修改了代码,需要重新 install 后才能在主项目中得到变化的效果

    方式二:pom聚合

    新建一个 maven 项目,在 pom.xml 中加入:

    <packaging>pom</packaging>
    <modules>
        <module>../c1</module>
        <module>../c2</module>
    </modules>

    module 节点就是要聚合的项目路径

    父子集成

    如果主项目引用了某依赖,假设B项目也要用该依赖,那么就需要在B项目中重复修改pom.xml写入依赖申明。
    此时我们可以让B项目继承主项目的依赖,在B项目中的 pom.xml 加入:

    <parent>
        <artifactId>txl</artifactId>
        <groupId>com.txl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    遇到的问题:

    1、提示 jre 必须运行在 jdk

    在maven中conf文件夹下 setting.xml 文件中的 profiles 节点下加入:

    <profile>    
        <id>jdk18</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>
  • 相关阅读:
    Vue 封装axios(四种请求)及相关介绍
    简单的按钮样式,两个连在一起的按钮
    http接口访问正常,https访问报错,基础连接已经关闭: 发送时发生错误.
    单点登录思路,多台服务器共用一个数据库,登录信息解决方案
    Wait()在过滤器中卡住 ,在异步代码上阻塞时的常见死锁问题
    接口对接,接口通过原有代码无法访问,解决办法,用postman解决
    svg基础标签说明
    server 2016部署网络负载平衡(NLB)
    写 JS 逻辑判断,不要只知道用 if-else 和 switch
    .NetCore部署IIS出错原因未安装ASP.N.NetCore部署IIS出错原因未安装ASP.NET Core Module v2ET Core Module v2
  • 原文地址:https://www.cnblogs.com/tangxuliang/p/9078948.html
Copyright © 2020-2023  润新知