• Maven笔记


    在cmd 中的命令:

    编译项目:mvn compile 

    把所有的@test文件都编译一下并生成测试报告:mvn test 

    清除文件以前编译生成的文件:mvn clean 

    一般在打包的时候都要进行clean一下

    打包:mvn package

    手动把本地的jar安装到本地的仓库:mvn install(优点可以把service和dao抽取出来当成一个项目,然后安装到本地,其他的项目可以直接导入该架包)

    打包的时候忽略test编译:mvn package -Dmave.test.skip=true 

    生成maven的骨架:mvn archetype:generate,然后即根据下面提示的信息进行设置groupid,artifactid,version,package

    ------------------------------------------------------------------

    Maven的scope范围:

    test:在测试的范围有效,在编译和打包的时候不会使用这个依赖

    compile :变异的时候有效,编译和打包的时候会进行依赖进去

    Provided:在编译和测试的时候有效,打包为war不会依赖进去,比如servle-api.jar ,因为这个包在tomcat中已经存在,如果再打包会有冲突

    Runtime:运行的时候依赖,但是在编译的时候不会进行依赖。

    默认的是compile 

    -------------------------------------------------

    maven 的传递性:

    b项目和c项目是独立的,并且进行mvn install生成jar,在a项目中引入b.jar,c.jar,这样的话a项目就拥有了b项目和c项目中的包。

    maven的层次性质:

    导入:

    <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>my.maven</groupId>
            <artifactId>user-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>my.maven</groupId>
            <artifactId>user-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>my.maven</groupId>
            <artifactId>user-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    如果b项目和c项目种分别引入了log4j这个包,但是版本是不一样的,这时候就看在pom文件中谁先引入,就是那个版本的包,而且还有层级的关系影响引入。

    除此之外导入项目包的时候,往往导入一个包就会传递很多的包进行,这时候我们可以相对性的忽略一些包的导入exclusions进行忽略

    聚合:

      使用一条命令就能操作很多的聚合项目:比如一个项目由user-service,user-dao,user-web来组成,其中service-web引入user-service和user-dao这两个项目,当我们编译项目的时候,不用一个一个进行编译,我们可以建立一个简单的maven项目(pom类型),其中简单的maven项目类型的pom文件如下配置:

      

    <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>my.maven</groupId>
      <artifactId>user-combine</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
      
      
      <!-- 聚合 -->
      <modules>
          <module>../user-dao</module>
          <module>../user-service</module>
          <module>../user-web</module>
      </modules>
      
      
      
      
    </project>

    这样的话我们就不用单个去操作当子项目了,我们可以操作user-combine这个项目进行编译,因为在user-combine项目中已经把上面的三个子项目聚合在一起了。

     继承:

      父项目pom:

      

    <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>my.maven</groupId>
      <artifactId>user-combine</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
      
      
      
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
        <junit.version>3.8.1</junit.version>  
      </properties>
      
      
      <!-- 聚合 -->
      <modules>
          <module>../user-core</module>
          <module>../user-dao</module>
          <module>../user-service</module>
          <module>../user-web</module>
      </modules>
      
      <!-- 继承的架包管理 -->
      <dependencyManagement>
          <dependencies>
              <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>${junit.version}</version>
            </dependency>
            
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            
          </dependencies>
          
      </dependencyManagement>
      
    </project>

    子项目pom:

      

    <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>
      <!-- 继承 -->
      <parent>
          <groupId>my.maven</groupId>
          <artifactId>user-combine</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <relativePath>../user-combine/pom.xml</relativePath>
      </parent>
      
      <!-- <groupId>my.maven</groupId> -->
      <artifactId>user-service</artifactId>
      <!-- <version>0.0.1-SNAPSHOT</version> -->
      <packaging>jar</packaging>
    
      <name>user-service</name>
      
      
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
      </dependencies>
    </project>
  • 相关阅读:
    代理模式和策略模式的区别
    代理模式vs适配器模式vs外观模式 转
    装饰模式与代理模式的区别(转载)
    用Delphi实现动态代理(2):设计说明  转
    Java静态内部类的介绍
    非常好的Java反射例子
    Java多线程中的锁机制
    Java多线程共享数据、同步、通信
    Java程序运行超时后退出解决
    Java安全:运用加密技术保护Java源代码
  • 原文地址:https://www.cnblogs.com/fucktom/p/5393625.html
Copyright © 2020-2023  润新知