简介:主要用于Windows和linux写相关插件时弄框架使用,基础的配置操作介绍,适合初学者和不喜欢被代码的人,本篇博客是自己整理的笔记,有不足的地方请多多指教!!!
maven操作配置目录
maven基础配置
主要为Maven的核心配置文件的配置和本地仓库的配置。
Maven的核心配置文件settings.xml
指定本地仓库的位置:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository -->
<localRepository>F:\installSoftware\BigDatas\apache-maven-3.5.4\data-repository</localRepository>
其中F:\installSoftware\BigDatas\apache-maven-3.5.4\data-repository为您自己的本地仓库地址
Maven的中央仓库配置
其默认配置是国外镜像,下载相应资源时特别慢,为此最好更换成国内源,这里更换成的源是阿里云
指定下载源
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
//镜像的id
<id>nexus-aliyun</id>
//镜像用来取代的远程仓库,central是中央仓库的id
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
//镜像的仓库地址
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
Maven的指定编译jdk版本配置
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<profile>
<id>jdk-1.8</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>
</profiles>
Maven打包的相关配置
在pox.xml中配置支持依赖打包的代码段
因为Maven默认不把依赖一起打包的,为此我们需要增加该配置
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在pom.xml配置依赖排除
为了确保程序正确可以将有可能重复的间接依赖排除。
添加如下代码即可
<dependencies>
<dependency>
<!--依赖排除-->
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
统一管理目标Jar包的版本
在实际开发中,往往存在jar包版本更新的情况,为此我们在配置时配置成同一管理目标会好很多
配置代码如下
<!--统一管理当前模块的jar包的版本-->
<project>
<properties>
<spring.version>4.0.0.RELEASE</spring.version>
</properties>
<!-- 以下<version>${spring.version}</version>中的${spring.version}取的值对应的就是:<spring.version>4.0.0.RELEASE</spring.version>中4.0.0.RELEASE值 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Maven的继承
使用继承机制就可以将这样的依赖信息统一提取到父工程模块中进行统一管理。
创建父工程
在父module中的pom.xml中添加如下代码:
<!-- 父工程的打包方式为pom -->
<project>
<groupId>com.lqs.maven</groupId>
<artifactId>Parent</artifactId>
<!--改变父模块的打包方式 改成pom 表示当前模块只做代码管理 不写任何代码-->
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
</project>
在子工程中引用父工程
相关代码如下:
<!-- 本代码段只是声明格式 -->
<parent>
<!-- 父工程坐标 -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<!--指定从当前pom.xml文件出发寻找父工程的pom.xml文件的相对路径-->
<relativePath>..</relativePath>
</parent>
<!-- 本代码段是使用例子 -->
<!--继承-->
<!-- 声明父模块-->
<project>
<parent>
<groupId>com.lqs.maven</groupId>
<artifactId>Parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--指定从当前pom.xml文件出发寻找父工程的pom.xml文件的相对路径-->
<relativePath>../Parent/pom.xml</relativePath>
</parent>
<!-- 注意:此时如果子工程的groupId和version如果和父工程重复则可以删除 -->
</project>
在父工程中管理依赖
即用dependencyManagement标签把Parent项目中的dependencies标签包裹起来
相关代码片段如下:
<project>
<!--依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 注意:如果在子项目中重新指定需要的依赖,删除范围和版本号
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> -->
</project>
Maven的聚合
配置聚合
在总的聚合工程中使用modules/module标签组合,指定模块工程的相对路径即可
相关代码如下:
<project>
<!--聚合-->
<modules>
<module>../MakeWorld</module>
<module>../OurWorlds</module>
<module>../HelloWorld</module>
<module>../Hello</module>
</modules>
</project>
Maven酷站
可以到http://mvnrepository.com/搜索需要的jar包的依赖信息。
http://search.maven.org/88