手动创建Maven项目并建立两个项目之间的依赖关系

用命令行快速建立maven项目

-> mvn:archetype:generate
-> 直接回车或者自己输入你想生成的
-> groupId
->artifactId
->如果有默认值回车即可
最后 y 确认创建

我们看下他的目录结构

项目名:

src
   ->main
         ->java
   ->test
         ->java
pom.xml
<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>xxx</groupId>
  <artifactId>xxx</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>xxxx</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

如何手动创建最简单的Maven项目并验证

在我创建了文件夹helloMaven后

建立pom.xml

<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>xxxx</groupId>
  <artifactId>helloMaven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>



</project>

发现就可以使用 mvn clean 即证明mvn项目仅需要pom.xml支持即可秉泽artifactId和文件夹名不需要相同

Maven的pom.xml可以配置哪些标签

标签部分转载自:https://www.cnblogs.com/sharpest/p/7738436.html

<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/maven-v4_0_0.xsd">
        <!-- 模型版本。maven2.0必须是这样写,现在是maven2唯一支持的版本 -->  
    <modelVersion>4.0.0</modelVersion>
     <!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.winner.trade,maven会将该项目打成的jar包放本地路径:/com/winner/trade -->  
    <groupId>gstd</groupId>
        <!-- 本项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->  
    <artifactId>wocpWeb</artifactId>
        <!-- 打包的机制,如pom,jar, maven-plugin, ejb, war, ear, rar, par,默认为jar -->  
    <packaging>war</packaging>
        <!-- 本项目目前所处的版本号 -->  
    <version>0.0.1-SNAPSHOT</version>
         <!--项目的名称, Maven产生的文档用 -->
    <name>gstd-wocpWeb Maven Webapp</name>
       <!--项目主页的URL, Maven产生的文档用 -->
    <url>http://maven.apache.org</url>
      <!--项目开发者属性-->
    <properties>
     <!-- 文件拷贝时的编码 -->  
     <!-- 可以在后文用${}取出便于全局控制,在很多情况下版本要统一如Spring -->  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
        <org.eclipse.jetty.version>8.0.3.v20111011</org.eclipse.jetty.version>
        <!-- Hibernate版本 -->
        <org.hibernate.version>3.6.8.Final</org.hibernate.version>
    </properties>
     <!--发现依赖和扩展的远程仓库列表。--> 
      <!--发现依赖和扩展的远程仓库列表。-->     
    <repositories>
         <!--包含需要连接到远程仓库的信息-->    
        <repository>
         <!--远程仓库唯一标识符。可以用来匹配在settings.xml文件里配置的远程仓库--> 
            <id>public</id>
               <!--远程仓库名称-->    
            <name>Public Repositories</name>
            <!--远程仓库URL,按protocol://hostname/path形式-->    
            <url>http://192.168.101.23:8081/nexus/content/groups/public/</url>
        </repository>
    </repositories>
 <!--该元素描述了项目相关的所有依赖。 这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载。要获取更多信息,请看项目依赖机制。-->    
    <dependencies>
    <!--这里一般有多个dependency-->
        <dependency>
           <!--依赖的group ID-->    
            <groupId>javax.mail</groupId>
            <!--依赖的artifact ID-->    
            <artifactId>mail</artifactId>
            <!--依赖的版本号。 在Maven 2里, 也可以配置成版本号的范围。-->    
            <version>1.4</version>
        </dependency>
    </dependencies>
 <!--构建项目需要的信息-->    
    <build>
      <!--产生的构件的文件名-->
        <finalName>gstd-wocpWeb</finalName>
        <!-- 通过过滤功能解析资源文件中的maven属性 -->
         <!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->    
        <resources>
           <!--这个元素描述了项目相关或测试相关的所有资源路径-->    
            <resource>
                <!--描述存放资源的目录,该路径相对POM路径-->    
                <directory>src/main/resources</directory>
                    <!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->    
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <filtering>false</filtering>
            </resource>
        </resources>
        <!-- 编译Java代码插件 -->
        <!--使用的插件列表 。--> 
        <plugins>
            <!--plugin元素包含描述插件所需要的信息。-->    
            <plugin>
                 <!--插件在仓库里的group ID-->    
                <groupId>org.apache.maven.plugins</groupId>
                     <!--插件在仓库里的artifact ID-->    
                <artifactId>maven-compiler-plugin</artifactId>
                 <!--扩展配置项-->    
                <configuration>
                    <encoding>utf-8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- skip test -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${org.eclipse.jetty.version}</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <useFileMappedBuffer>false</useFileMappedBuffer>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>${wocp.server.start.startport}</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                    <stopKey>${wocp.server.start.stopkey}</stopKey>
                    <stopPort>${wocp.server.start.stopport}</stopPort>
                    <systemProperties>
                        <systemProperty>
                            <name>org.mortbay.jetty.Request.maxFormContentSize</name>
                            <value>1000000</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </plugin>

            <!-- js、css压缩 -->


            <!-- 利用assembly插件打包 -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/assembly/package.xml</descriptor>
                    </descriptors>
                     <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->   
                    <executions>
                          <!--execution元素包含了插件执行需要的信息-->    
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                             <!--配置的执行目标-->    
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <!-- 针对不同环境的profile -->
     <!--在列的项目构建profile,如果被激活,会修改构建处理-->  
    <profiles>
        <!-- 开发配置 -->
          <!--根据环境参数或命令行参数激活某个构建处理-->  
        <profile>
            <id>dev</id>
            <properties>
                <wocp.server.start.startport>9200</wocp.server.start.startport>
                <wocp.server.start.stopport>9201</wocp.server.start.stopport>
                <wocp.server.start.stopkey>stop</wocp.server.start.stopkey>
                <bill.db.driver>oracle.jdbc.OracleDriver</bill.db.driver>
                <bill.db.url>jdbc:oracle:thin:@192.168.101.23:1521:nfc</bill.db.url>
                <bill.db.username>bill_center</bill.db.username>
                <bill.db.passwd>bill</bill.db.passwd>
                <shine.db.driver>oracle.jdbc.OracleDriver</shine.db.driver>
                <shine.db.url>jdbc:oracle:thin:@192.168.101.23:1521:nfc</shine.db.url>
                <shine.db.username>shine_center</shine.db.username>
                <shine.db.passwd>shine</shine.db.passwd>
            </properties>
        </profile>
        <!-- 连接测试库的配置 -->
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <wocp.server.start.startport>5180</wocp.server.start.startport>
                <wocp.server.start.stopport>5181</wocp.server.start.stopport>
                <wocp.server.start.stopkey>stop</wocp.server.start.stopkey>
                <bill.db.driver>oracle.jdbc.OracleDriver</bill.db.driver>
                <bill.db.url>jdbc:oracle:thin:@192.168.1.66:1521:orcl</bill.db.url>
                <bill.db.username>dev_bill_smc</bill.db.username>
                <bill.db.passwd>abc</bill.db.passwd>
                <shine.db.driver>oracle.jdbc.OracleDriver</shine.db.driver>
                <shine.db.url>jdbc:oracle:thin:@192.168.1.66:1521:orcl</shine.db.url>
                <shine.db.username>dev_shine_smc</shine.db.username>
                <shine.db.passwd>abc</shine.db.passwd>
            </properties>
        </profile>
        <!-- 生产配置 -->
        <profile>
            <id>product</id>
            <properties>
            </properties>
        </profile>
    </profiles>
</project>

一点一点学maven(06)——POM.XML文件常用标签使用介绍](http://blog.csdn.net/javaloveiphone/article/details/52080886)

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

  <!-- 指定了当前POM的版本 -->
  <modelVersion>4.0.0</modelVersion>

  <!-- 项目坐标信息 -->
  <!-- 项目主标识,用于定义当前项目属于的实际项目,格式与项目创建的包是一样的,公司域名反写-->
  <groupId>com.jsun.demo</groupId>
  <!-- 项目名或模块名或项目名+模块名组成 -->
  <artifactId>demo-maven01</artifactId>
  <!-- 当前项目版本号,一般由三个数字组成,第一个0表示大版本号,第二个0表示分支版本号,第三个1表示小版本号 -->
  <!-- SNAPSHOT代表当前版本类型为快照版本,还有alpha内部版本、beta公测版本、release发布版本、ga正式版本等 -->
  <version>0.0.1-SNAPSHOT</version>
  <!-- maven打包方式,默认为jar,还有:pom,maven-plugin,war,rar,zip -->
  <packaging>jar</packaging>

  <!-- 用在子模块中,实现对父模块的继承 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
  </parent>

  <!-- 聚合多个maven项目,同时对所有聚合项目进行编译 -->
  <modules>
    <module></module>
  </modules>

  <!-- 项目描述名,url,详细描述,产生项目文档使用 -->
  <name>Maven01</name>
  <url>http://maven.apache.org</url>
  <description>测试maven项目</description>

  <!-- 开发人员列表,项目发布使用 -->
  <developers>
    <!-- 某个项目开发者的信息 -->
    <developer>
        <!-- 项目开发者的唯一标识符 -->
        <id>001</id>
        <!-- 项目开发者的全名 -->
        <name>jsun</name>
        <!-- 项目开发者的email -->
        <email> jsun@163.com </email>
        <!-- 项目开发者的主页的URL -->
        <url />

        <!-- 项目开发者在项目中扮演的角色,角色元素描述了各种角色 -->
        <roles>
            <role>developer</role>
        </roles>

        <!-- 项目开发者所属组织 -->
        <organization>com-jsun</organization>
        <!-- 项目开发者所属组织的URL -->
        <organizationUrl> http://demo.jsun.com/jsun</organizationUrl>   
    </developer>
  </developers>


  <!-- 许可证信息, -->
  <licenses>
    <license>
        <name></name>
        <!-- 官方的license正文页面的URL -->
        <url></url>
        <!-- 项目分发的主要方式:repo,可以从Maven库下载,manual,用户必须手动下载和安装依赖 -->
        <distribution></distribution>
        <!-- 关于license的补充信息 -->
        <comments></comments>
    </license>
  </licenses>

  <!-- 项目所属组织信息 -->
  <organization>
      <name></name>
      <url></url>
  </organization>


  <!-- 属性列表,相当于定义的公共常量,引用方式比如:${project.build.sourceEncoding} -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>3.8.1</junit.version>
  </properties>

  <!-- 依赖列表 -->
  <dependencies>
    <!-- 具体依赖项,下面主要包含依赖的坐标、类型、范围等信息 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>1.2.6</version>

      <!-- 依赖的类型 -->
      <type>jar</type>


      <!-- 项目如果要使用某个框架或依赖,需要把相关jar包引用到classpath中,maven项目提供了三个classpath:编译、测试、运行 -->
      <!-- 依赖的范围用于控制依赖于三种classpath关系的,包括:compile、provided、runtime、test、system、import -->
      <!-- 
        compile:默认范围,编译、测试、运行都有效
        provided:编译和测试有效,最后运行不会被加入
        runtime:在测试和运行的时候有效,编译不会被加入,比如jdbc驱动jar
        test:测试阶段有效,比如junit
        system:与provided一致,编译和测试阶段有效,但与系统关联,可移植性差
        import:导入的范围,它只是用在dependencyManagement中,表示从其它的pom中导入dependency的配置
       -->
      <!-- 表示当前依赖只能在测试代码中引用使用,在主代码中引用使用则报错 -->
      <scope>test</scope>


      <!-- 排除依赖传递列表,比如A依赖B,B依赖C,但是A并没有使用C的功能,可以把C排除-->
      <exclusions>
        <exclusion></exclusion>
      </exclusions>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!-- 主动设置禁止自己被传递,只在当前项目中使用 -->
            <optional>true</optional>
        </dependency>

    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <!-- 在相同版本下针对不同的环境或者jdk使用的jar,如果配置了这个元素,则会将这个元素名在加在最后来查找相应的jar,
        具体解释查看:http://www.cnblogs.com/lovingprince/archive/2010/09/19/2166273.html -->
        <classifier>jdk15</classifier>
        <version>2.4</version>
    </dependency>
  </dependencies>

  <!-- 使用dependencyManagement标签管理依赖,实际管理的是依赖的版本号,让
所有子项目中引用对应依赖而不用显式的列出版本号;
依赖并不会在当前项目引入 -->
  <dependencyManagement>
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>      
        </dependency>
    </dependencies>
  </dependencyManagement>

  <!-- 构建插件 -->
  <build>
    <!-- 
        Maven定制化打包后的包名
        Maven默认的包名为:<finalName>${project.artifactId}-${project.version}</finalName>
        定制化想要的包名,如加上时间戳:<finalName>${project.artifactId}-${maven.build.timestamp}</finalName>
    -->
    <finalName>myProject</finalName>  

    <!-- 插件列表 -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <!-- 插件管理列表,与dependencyManagement标签作用相似,管理插件版本号,让子项目继承使用 -->
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- 插件扩展配置 -->
                <!-- 更详细的例子:http://my.oschina.net/zh119893/blog/276090 -->
                <configuration>
                    <!-- 源代码编译版本 -->
                    <source>1.7</source>
                    <!-- 目标平台编译版本 -->
                    <target>1.7</target>
                    <!-- 设置编译字符集编码 -->
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

        </plugins>
    </pluginManagement>
  </build>
</project>

所以经过总结可以看出Maven的pom.xml中大致有以下几种:

<properties> //配置全局统一的配置
<repositories> //配置远程仓库 但是一般使用默认maven
<dependencies> //用到最多的项目依赖
<build> //构建项目涉及到的   <resources>,<plugin>

如何建立两个Maven项目之间的依赖关系

如果A项目依赖B项目,打开A项目的pom.xml 在期中录入B项目的信息即可

<dependencies>
        <dependency>
            <groupId>xxxx</groupId>
            <artifactId>mavenPom</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Maven项目的依赖关系有哪些

项目的依赖关系主要分为三种:依赖,继承,聚合

Maven之(九)依赖关系

附录

在实际使用调用自己项目依赖时需要先进行

mvn install

解决Maven项目相互依赖/循环依赖/双向依赖的问题 http://blog.csdn.net/leolu007/article/details/53079875

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值