Maven 构建知识库
1. 安装本地jar或者pom到构建仓库
- mvn install:install-file -Dfile=cs-1.1.3-SNAPSHOT.jar -DgroupId=com.bireturn -DartifactId=cs -Dversion=1.1.3-SNAPSHOT -Dpackaging=jar
- mvn install:install-file -Dfile=cs-1.1.3-SNAPSHOT.pom -DgroupId=com.bireturn -DartifactId=cs -Dversion=1.1.3-SNAPSHOT -Dpackaging=pom
复制代码 2. Maven依赖当前项目相对jar
- <dependency>
- <version>1</version>
- <groupId>com.herbert</groupId>
- projectLocal.jar</artifactId>
- <scope>system</scope>
- <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/projectLocal.jar</systemPath>
- </dependency>
复制代码 3. Maven构建编译依赖本地jar
- <plugin>
-
- <groupId>org.apache.maven.plugins</groupId>
- maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <compilerArguments>
- <extdirs>${basedir}/src/main/resources/lib/bo;${basedir}/src/main/resources/lib/cognos</extdirs>
- </compilerArguments>
- </configuration>
- </plugin>
复制代码 4. Maven构建运行外部程序
- <plugin>
-
- <groupId>org.codehaus.mojo</groupId>
- exec-maven-plugin</artifactId>
- <version>3.2.0</version>
- <executions>
- <execution>
- <id>copy api to others project</id>
- <phase>package</phase>
- <goals>
- <goal>exec</goal>
- </goals>
- <configuration>
- <executable>cmd</executable>
-
- /c echo done copy version .</argument>
- && copy /Y ${project.build.directory}\${project.artifactId}.jar D:\\lib\${project.artifactId}.jar </argument>
- </arguments>
- </configuration>
- </execution>
- </executions>
- </plugin>
复制代码 5. Maven构建运行class程序
- <plugin>
-
- <groupId>org.codehaus.mojo</groupId>
- exec-maven-plugin</artifactId>
- <version>3.2.0</version>
- <executions>
- <execution>
- <id>convert version code</id>
- <phase>prepare-package</phase>
- <goals>
- <goal>java</goal>
- </goals>
- <configuration>
- <includeProjectDependencies>true</includeProjectDependencies>
- <includePluginDependencies>true</includePluginDependencies>
- <mainClass>com.herbert.common.HkDoConvertVersionCode</mainClass>
-
- 微信小游戏地心侠士</argument>
- </arguments>
- </configuration>
- </execution>
- </executions>
- </plugin>
复制代码 6. Maven构建自定义zip文件
- <plugin>
-
- <groupId>org.apache.maven.plugins</groupId>
- maven-assembly-plugin</artifactId>
- <version>3.3.0</version>
- <executions>
- <execution>
- <id>rootZip-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- <configuration>
- <finalName>${name}</finalName>
- false</appendAssemblyId>
- <descriptors>
- <descriptor>./rootZip-assembly.xml</descriptor>
- </descriptors>
- </configuration>
- </execution>
- </executions>
- </plugin>
复制代码 rootZip-assembly.xml 文件内容- <id>bin-1</id>
- <formats>
- <format>zip</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <fileSets>
- <fileSet>
- <directory>src/main/webapp/WEB-INF</directory>
- <outputDirectory>/herbert/WEB-INF</outputDirectory>
- <excludes>
- <exclude>cfgHome/hkInitPath/initCmd/**</exclude>
- <exclude>cfgHome/hkInitPath/command/*.command</exclude>
- </excludes>
- </fileSet>
- <fileSet>
- <directory>target/classes</directory>
- <outputDirectory>/herbert/WEB-INF/classes</outputDirectory>
- </fileSet>
- <fileSet>
- <directory>src/main/webapp/apps_res</directory>
- <outputDirectory>/herbert/apps_res</outputDirectory>
- </fileSet>
- <fileSet>
- <directory>src/scripts</directory>
- <outputDirectory>/</outputDirectory>
- <filtered>true</filtered>
- </fileSet>
- </fileSets>
- <dependencySets>
- <dependencySet>
- <outputDirectory>/lib</outputDirectory>
- <scope>runtime</scope>
- <excludes>
- <exclude>${project.groupId}:${project.artifactId}</exclude>
- </excludes>
- </dependencySet>
- <dependencySet>
- <outputDirectory>/</outputDirectory>
- <includes>
- <include>${project.groupId}:${project.artifactId}</include>
- </includes>
- </dependencySet>
- </dependencySets>
- </assembly>
复制代码 复制文件时可以对某些文件内容做替换,比如src/scripts 文件中的一个文件内容,打包后,对应参数会替换成具体的值- java -jar -Dloader.path=resources,lib ${project.artifactId}-${project.version}.jar shutdown
复制代码 7. 筛选项目不同的class单独构建一个jar文件
- <plugin>
-
- <groupId>org.apache.maven.plugins</groupId>
- maven-jar-plugin</artifactId>
- <executions>
- <execution>
- <id>api</id>
- <phase>package</phase>
- <goals>
- <goal>jar</goal>
- </goals>
- <configuration>
- <classifier>herbert-dxxs-api</classifier>
-
- <manifest>
- false</addClasspath>
- </manifest>
- </archive>
- <includes>
- <include>**/com/dxxs/DataImport*.class</include>
- <include>**/com/dxxs/DeleteFile*.class</include>
- <include>**/com/dxxs/DownFile*.class</include>
- <include>**/com/dxxs/ExcelConvert*.class</include>
- <include>**/com/dxxs/FileListener*.class</include>
- <include>**/com/dxxs/FileUpaload*.class</include>
- <include>**/com/dxxs/UploadFileInfo*.class</include>
- </includes>
- </configuration>
- </execution>
- </executions>
- </plugin>
复制代码 8. Maven构建的一些全局变量
- 内置属性(Maven预定义属性,用户可以直接使用)
- ${basedir}表示项目的根路径,即包含pom.xml文件的目录
- ${version}表示项目版本
- ${project.basedir}同$
- ${project.baseUri}表示项目文件地址
- ${maven.build.timestamp}表示项目构建开始时间
- ${maven.build.timestamp.format}表示${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm
- pom属性(使用pom属性可以引用到pom.xml文件中对应元素的值)
- ${project.build.sourceDirectory}表示主源码路径,默认为src/main/java/
- ${project.build.testSourceDirectory}表示测试源码路径,默认为src/test/java/
- ${project.build.directory}表示项目构建输出目录,默认为target/
- ${project.outputDirectory}表示项目测试代码编译输出目录,默认为target/classes/
- ${project.groupId}表示项目的groupId
- ${project.artifactId}表示项目的artifactId
- ${project.version}表示项目的version,同$
- ${project.build.finalName}表示项目打包输出文件的名称,默认为${project.artifactId}$
- 自定义属性(在pom.xml文件的标签下定义的Maven属性)
定义属性:- <project>
- <properties>
- <mysql.version>5.6.32</mysql.version>
- </properties>
- </project>
复制代码 使用属性:- <dependency>
- <groupId>mysql</groupId>
- mysql-connector-java</artifactId>
- <version>${mysql.version}</version>
- </dependency>
复制代码
- setting.xml文件属性(与pom属性同理,用户可以用settings.开头的属性引用setting.xml文件的xml元素值)
- ${settings.localRepository}表示本地仓库的地址
- Java系统属性(所有的Java属性都可以使用Maven属性引用)
- mvn help:system 可以查看所有的Java属性
System.getProperties() 可以得到所有的Java属性
${user.home} 表示用户目录
- 环境变量属性(所有的环境变量都可以以env.开头的Maven属性引用)
- mvn help:system 可查看所有的环境变量
- ${env.JAVA_HOME} 表示JAVA_HOME环境变量的值
9. Maven只构建父工程或只构建子工程
只构建父工程
mvn package -N
只构建子工程,多个子工程逗号分隔
mvn package -pl 子模块名称
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |