maven项目的创建与配置、maven命令行操作、参数说明、依赖管理

5 篇文章 0 订阅

一、建maven项目(eclipse ee版本)

选这两项就可以建maven项目了

二、设置maven

一是修改setting.xml(maven的conf目录)

修改本地仓库地址:<localRepository>d:/.m2/repository</localRepository>

在mirrors加上阿里云的镜像:

<mirror>
        <!--This sends everything else to /public -->
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

三、查询maven包:

http://mvnrepository.com/

找到相关包,比如junit:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
在maven项目的pom.xml的dependencies加上这个depend即可

四、建立测试类小技巧:

选中主类,建立junit类:

然后拖到test目录下。

五、maven命令

1,mvn -v 查看版本信息,

2,mvn compile 编译  在项目根目录下

3,mvn test运行测试文件

4,mvn package 对项目进行打包

5,mvn clean 删除target目录

6,mvn install 安装项目jar包到本地仓库

7,archetype 插件:创建符合maven规定的项目骨架

mvn archetype:generate  会在空的目录中建立maven项目

Choose a number or apply filter,7建立quick start项目

然后填入group-id等,也可以如下方式直接指定

D:\workspace\m3>mvn archetype:generate -DgroupId=commaven -DartifactId=
maven-test01 -Dversion=0.0.1-snapshot -Dpackage=com.maven.test

 

六、参数说明(pom.xml)

modelVersion:当前pom的版本号

groupId:组织名,公司名网址反过来写+项目名称

artifactId:项目名+模块名

name:也是项目名,如果不填,默认是artifactId

version:版本号  

   0.0.1-SNAPSHOT  第一个数表示大版本好,第二个数表示分支版本号,第三个数表示小版本号

   SNAPSHOT  :快照版本,开发阶段

   alpha:内部测试版本

   beta:公测版本

   release:稳定版本

   package:代码的包路径

   GA:正式发布

dependency scope:作用域(http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Dependency Scope

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

There are 6 scopes available:

  • compile    默认范围,整个编译测试运行阶段有效
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided     编译测试  有效
    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime    运行  测试有效
    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test          只在测试阶段  有效
    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
  • system      跟本机系统相关联   可一致性差
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import       导入的范围,在 dependencyManagement中,表示从其他的pom中导入dependency的配置作用
    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

这个scope在继承父工程事是很有用的啦,比如你用了parent继承一个父工程,但是dependency不写版本号就报错,这时,加上type为pom,scope为import就可以了(在另一篇spring boot整合测试中会进行说明)。

packaging :有jar、war、zip、pom等方式打包

url:项目地址

description:项目描述

optiona :设置依赖是否可选

exclusions:排除依赖传递列表

dependencyManagement:进行依赖管理

plugins:插件管理

parent:父级

modules、module:模块

scope:依赖作用域范围

 

七,maven的生命周期

clean compile test package intall

完成的项目构建过程

清理  编译  测试  打包  继承测试     --部署到服务器

Clean  清理项目

     pre-clean:执行清理前的工作

     Clean:清理上一次构建产生的所有文件

     Post-clean:执行清理后的工作

Default   构建项目(核心)

     Compile   \  test   、 package 、instal

site   生成站点  进行访问

    Pre-site:项目生成站点的工作

    Site:生成网站文件

    Post-site:生成后的工作

    Site-deploy:发布到服务器

    

八、maven的依赖冲突及管理

1,谁离得近,谁优先,

           a)son  ->mother  ->  father  -> x-jar包

                    如果father和mother都有x.jar,则继承mother的包

           b)son  ->mother  ->x.jar包

2,谁先声明谁优先

         谁在前,用谁的依赖

        son  ->mother ->x.jar包

        son  ->father ->x.jar包

 

九、maven的聚合和继承

1,maven的聚合,就是多模块的组合。聚合的项目的pom注意配置两个地方:

  <!-- 聚合项目需要用pom -->
  <packaging>pom</packaging>

<!-- 模块部分 -->
  <modules>
      <module>../yilai-mother</module>
      <module>../father</module>
      <module>../son</module>
  </modules>

配置完后,我个人的感觉就是一导包全都能导进来

2,依赖的继承,如下语句即为继承:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

用eclipse依次点开,会找到一堆的依赖版本。个人感觉parent的主要作用就在于统一版本信息。继承的依赖可以从父依赖的<dependencyManagement>版本信息

十、profile设置

有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等

每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到maven的profile配置了

配置内容大概如下:

 <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>

其中id代表这个环境的唯一标识,下面会用到
properties下我们我们自己自定义了标签env,内容分别是dev和prd。

activeByDefault=true代表如果不指定某个固定id的profile,那么就使用这个环境

2,下面是我们resources下的目录,有两个目录,dev和prd,在开发时,我们使用dev下的配置文件,部署时候使用prd下的配置文件

 

 

3配置pom.xml,如果直接install,那么就会找到默认的id为dev的这个profile,然后会在里面找env的节点的值,

接着就会执行替换,相当于将src/main/resources/dev这个文件夹下的所有的配置文件打包到classes根目录下

    <build>
        <finalName>springmvc2</finalName>
        <resources>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>

 

 在idea下指定打包时指定profile的id

1第一步

 2第二步

 

3第三步

4第四步,执行打包命令

 

这个时候target下springmvc项目下的classes根目录下有了env.properties,并且里面内容是我们指定的那个env.properties,

但是发现resources下的aa.properties文件没有被打包进去,那是因为我们只指定了resources/prd下的文件打包到根目录下,并没有指定其他文件

我们在pom.xml下的resources节点下新增规则

<resource>
    <directory>src/main/resources</directory>
    <excludes>
        <exclude>dev/*</exclude>
        <exclude>prd/*</exclude>
    </excludes>
</resource>

 

再执行打包就会将resources下的除了dev文件夹和prd文件夹的其他所有文件打包到classes根目录下了

 

 

最后注意:如果有其他配置文件在src/main/java目录下,也是可以这样指定的,但是要指定

<includes>
    <include>*.xml</include>
</includes>

不然会将java类当配置文件一块放到classes根目录下,加了include就会只匹配符合条件的放到target的classes根目录下

 最后放我的所有的配置

   <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>


    <build>
        <finalName>springmvc</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>prd/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值