1.问题描述
maven pom.xml中maven-assembly-plugin报红,无法加载
2.pom文件build构建部分详解
<build>
<!--当项目没有规定目标(Maven2 叫做阶段)时的默认值-->
<defaultGoal>install</defaultGoal>
<!--build目标文件的存放目录,默认在 ${basedir}/target 目录-->
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
<!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->
<resources>
<!--这个元素描述了项目相关或测试相关的所有资源路径-->
<resource>
<!-- 描述了资源的目标路径。该路径相对target/classes目录(例如${project.build.outputDirectory})。举个例 子,如果你想资源在特定的包里( org.apache.maven.message,你就必须该元素设置为org/apache/maven /messages。然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。-->
<targetPath/>
<!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->
<filtering/>
<!--描述存放资源的目录,该路径相对POM路径-->
<directory/>
<!--包含的模式列表,例如**/*.xml.-->
<includes/>
<!--排除的模式列表,例如**/*.xml-->
<excludes/>
</resource>
</resources>
<!--这个元素描述了单元测试相关的所有资源路径,例如和单元测试相关的属性文件。-->
<testResources>
<!--这个元素描述了测试相关的所有资源路径,参见build/resources/resource元素的说明-->
<testResource>
<targetPath/>
<filtering/>
<directory/>
<includes/>
<excludes/>
</testResource>
</testResources>
<!-- 在build时,执行的插件,比较有用的部分,如使用jdk 8.0编译等等-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<tagBase>${git.conn}</tagBase>
<branchBase>${git.conn}</branchBase>
<username>${git.username}</username>
<password>${git.password}</password>
</configuration>
</plugin>
...
</plugins>
<!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置-->
<pluginManagement>
<plugins>
...
</plugins>
</pluginManagement>
</build>
</project>
3.pom文件dependencies依赖部分详解
依赖关系:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
groupId, artifactId, version:描述了依赖的项目唯一标志
可以通过以下方式进行安装:
使用以下的命令安装:
mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1
创建自己的库,并配置,使用deploy:deploy-file
设置此依赖范围为system,定义一个系统路径。不提倡。
type:相应的依赖产品包形式,如jar,war
scope:用于限制相应的依赖范围,包括以下的几种变量:
compile :默认范围,用于编译
provided:类似于编译,但支持你期待jdk或者容器提供,类似于classpath
runtime:在执行时,需要使用
test:用于test任务时使用
system:需要外在提供相应得元素。通过systemPath来取得
systemPath: 仅用于范围为system。提供相应的路径
optional: 标注可选,当项目自身也是依赖时。用于连续依赖时使用
独占性
外在告诉maven你只包括指定的项目,不包括相关的依赖。此因素主要用于解决版本冲突问题
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
表示项目maven-embedder需要项目maven-core,但我们不想引用maven-core
3.pom文件其他标签描述
继承关系
一个强大的变化,maven带来的是项目继承。主要的设置:
定义父项目
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.gene.demo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
</project>
packaging 类型,需要pom用于parent和合成多个项目。我们需要增加相应的值给父pom,用于子项目继承。主要的元素如下:
依赖型
开发者和合作者
插件列表
报表列表
插件执行使用相应的匹配ids
插件配置
子项目配置
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gene.demo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
</project>
relativePath可以不需要,但是用于指明parent的目录,用于快速查询。
dependencyManagement:
用于父项目配置共同的依赖关系,主要配置依赖包相同因素,如版本,scope。
合成(或者多个模块)
一个项目有多个模块,也叫做多重模块,或者合成项目。
如下的定义:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.gene.demo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<modules>
<module>my-project1</module>
<module>my-project2</module>
</modules>
</project>
4.解决方案
由于,build中插件无法自动加载,在依赖中添加对应依赖更新后即可成功加载。
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>