maven学习笔记
1. maven项目目录结构
idea新建maven工程,结构如图:
maven官网给出的目录结构标准:
${basedir} |-- pom.xml // Maven build项目的配置信息文件
|-- src | |-- main | | `-- java // 项目源代码目录 | | `-- resources // 项目资源文件目录 | | `-- filters // 项目资源过滤文件目录 | | `-- webapp // web应用源代码目录 | |-- test // 测试目录 | | `-- java | | `-- resources | | `-- filters | |-- it | |-- assembly //打包描述文件目录
| |-- site |-- LICENSE.txt |-- NOTICE.txt |-- README.txt
2. pom.xml配置
pom文件位于工程根路径下,包含了项目配置细节信息,maven基于这些信息构建项目。
以实际项目为例,项目目录为:
${basedir} |-- pom.xml // 父pom文件 |-- gatekeeper-tool | |-- pom.xml | |-- src | | |-- main | | |-- test | | |-- assembly |-- gatekeeper-web | |-- pom.xml | |-- src | | |-- main | | |-- test | | |-- assembly | | | `-- assembly.xml | | | `-- assembly-2.0.0.xsd
父pom文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 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:artifactId:version--> <groupId>com.eageldiao</groupId> <artifactId>gatekeeper</artifactId> <version>0.0.1</version> <packaging>pom</packaging> <modules> <module>gatekeeper-web</module> <module>gatekeeper-tool</module> </modules> </project>
子模块项目中的pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion>
<!-- 父pom--> <parent> <groupId>com.eaglediao</groupId> <artifactId>gatekeeper</artifactId> <version>0.0.1</version> </parent> <artifactId>gatekeeperweb</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>4.3.7.RELEASE</spring.version> <spring.security.version>4.2.2.RELEASE</spring.security.version> <maven.test.skip>false</maven.test.skip> <hibernate.version>5.4.1.Final</hibernate.version> <log4j.version>2.7</log4j.version> <jackson.version>2.8.6</jackson.version> </properties> <dependencies> <!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> ...略 </dependencies>
<build> <defaultGoal>install</defaultGoal> <finalName>${project.version}</finalName> <resources> <resource> <directory>src/main/webapp</directory> <targetPath>webapp</targetPath> </resource> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/conf</directory> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/assembly/assembly.xml</descriptor> </descriptors> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <includes> <include>com/**</include> </includes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> </plugin> </plugins> </build> </project>
3. assembly.xml配置
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id>release</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <includes> <include>*:*:*:*</include> </includes> <outputDirectory>lib</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> </dependencySet> </dependencySets>
<fileSets> <fileSet> <directory>src/main/conf</directory> <outputDirectory>witeco-conf</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>witeco-conf</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> <lineEnding>unix</lineEnding> </fileSet> <fileSet> <directory>src/main/webapp</directory> <outputDirectory>webapp</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> </fileSet> <!-- bin目录下启动脚本 --> <fileSet> <directory>src/main/script/startup</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> <lineEnding>unix</lineEnding> </fileSet> <!-- tool目录下工具脚本 --> <fileSet> <directory>src/main/script/tool</directory> <outputDirectory>tool</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> <lineEnding>unix</lineEnding> </fileSet> <!-- tool目录下jre --> <fileSet> <directory>src/main/tool/jre</directory> <outputDirectory>tool/jreTmp</outputDirectory> <fileMode>0755</fileMode> <directoryMode>0755</directoryMode> </fileSet> </fileSets> </assembly>
eaglediao