首页 > 其他分享 >手动创建Maven项目并建立两个项目之间的依赖关系

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

时间:2022-12-13 17:36:24浏览次数:52  
标签:maven http 4.0 项目 手动 Maven apache org


用命令行快速建立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可以配置哪些标签

<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>
<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> [email protected] </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,
<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>
<!-- 插件扩展配置 -->
<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,4.0,项目,手动,Maven,apache,org
From: https://blog.51cto.com/u_11061155/5934914

相关文章

  • idea启动项目(本地jar包)的配置
    一、nohupjava-jarjarName-0.0.1-SNAPSHOT.jar>msg.log2>&1&;java-Xms256m-Xmx512m-XX:PermSize=128M-XX:MaxPermSize=256M-jara.jar分配内存nohupjava-X......
  • 新建项目及语法结构
    1.新建一个项目1.新建工作目录:xxx2.目录内新建文件夹:bin,pkg,src3.初始化:gomodinitsrc4.环境变量:exportGOPATH=/Users/..../..../go_workspaceexportGOBIN=$GOPAT......
  • 世界杯竞猜项目Dapp-第三章(ERC20)
    ERC20是标准的以太坊Token协议,它也是一个合约代码,只要在该合约内部实现了特定的6个方法,就会被系统判定为代币合约,具体总结为:6个必要接口,2个必要事件,3个可选接口,详......
  • redis缓存穿透、缓存雪崩、缓存击穿【项目总结】
    Redis项目总结--缓存穿透、缓存雪崩、缓存击穿目录Redis项目总结--缓存穿透、缓存雪崩、缓存击穿一.缓存穿透1.什么是缓存穿透2.缓存穿透解决方案3.流程二.缓存雪崩1.什么......
  • vue项目自动导入components
    开发项目中一般组件都放在components目录下,对于一些高频使用的组件我们需要在入口文件中设置为全局组件,一个一个搞,很繁琐,这里通过webpack自动挂载components为全局组件......
  • 城院书集-项目原型设计
    一、开发工具前端app端:微信开发者工具数据库:微信云开发开发工具:vscode二、前端app前端app(微信小程序):登录后可使用功能 ......
  • 手动build unity3d的docker镜像
    手动buildunity3d的docker镜像参考资料docker官方文档:​​DockerDocumentation|DockerDocumentation​​unity3dlinux版的论坛链接,在这里能找到各个版本,以及需要安装......
  • 【Unity】超级坦克大战(一)搭建项目、导入框架、前期开发准备
    更新日期:2020年7月9日。项目源码:在终章发布免责声明:超级坦克大战使用的图片、音频等所有素材均有可能来自互联网,本专栏所有文章仅做学习和教程目的,不会将任何素材用于任何......
  • windows搭建maven私服nexus仓库并且上传本地maven库jar包
    windows搭建maven私服nexus仓库并且上传本地maven库jar包一、maven私服仓库nexus搭建nexus下载1.首先需要从官网下载nexus安装包地址:https://www.sonatype.com/downloa......
  • Maven打包部署
    Maven打Jar包问题一Eclipse突然SB告诉我类不存在还让我导包进来O__O"…,但是我明明有这个类。这是不要慌,通过在网上搜索得知:​​​由于eclipse的编译是基于时间戳的判断机制......