知乎专栏 |
配置默认的jdk版本
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion> </properties>
在插件中配置
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
禁止编译警告 -Xlint:unchecked,-Xlint:deprecation
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <compilerArgs> <arg>-verbose</arg> <arg>-Xlint:unchecked</arg> <arg>-Xlint:deprecation</arg> </compilerArgs> </configuration> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <compilerArgs> <arg>-verbose</arg> <arg>-Xlint:unchecked</arg> <arg>-Xlint:deprecation</arg> <arg>-bootclasspath</arg> <arg>${env.JAVA_HOME}/jre/lib/rt.jar</arg> <arg>-extdirs</arg> <arg>${project.basedir}/libs</arg> </compilerArgs> </configuration> </plugin>
compilerArgs可以实现编译参数的传递
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <!-- 指定maven编译的jdk版本 --> <!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 --> <source>1.8</source> <!-- 源代码使用的JDK版本 --> <target>1.8</target> <!-- 需要生成的目标class文件的编译版本 --> <encoding>UTF-8</encoding><!-- 字符集编码 --> <skipTests>true</skipTests><!-- 跳过测试 --> <verbose>true</verbose> <showWarnings>true</showWarnings> <fork>true</fork><!-- 要使compilerVersion标签生效,还需要将fork设为true,用于明确表示编译版本配置的可用 --> <executable><!-- path-to-javac --></executable><!-- 使用指定的javac命令,例如:<executable>${JAVA_1_4_HOME}/bin/javac</executable> --> <compilerVersion>1.3</compilerVersion><!-- 指定插件将使用的编译器的版本 --> <meminitial>128m</meminitial><!-- 编译器使用的初始内存 --> <maxmem>512m</maxmem><!-- 编译器使用的最大内存 --> <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument><!-- 这个选项用来传递编译器自身不包含但是却支持的参数选项 --> </configuration> </plugin>
设置 war 文件名 warName
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warName>bird.war</warName> </configuration> </plugin> </plugins> </build> ... </project>
查看可用的pom定义
<project> <modelVersion>4.0.0</modelVersion> <artifactId>my-test-app</artifactId> <groupId>my-test-group</groupId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <configuration> <target> <property name="compile_classpath" refid="maven.compile.classpath"/> <property name="runtime_classpath" refid="maven.runtime.classpath"/> <property name="test_classpath" refid="maven.test.classpath"/> <property name="plugin_classpath" refid="maven.plugin.classpath"/> <echo message="compile classpath: ${compile_classpath}"/> <echo message="runtime classpath: ${runtime_classpath}"/> <echo message="test classpath: ${test_classpath}"/> <echo message="plugin classpath: ${plugin_classpath}"/> <ant antfile="${basedir}/build.xml"> <target name="test"/> </ant> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> The build.xml: <?xml version="1.0"?> <project name="test6"> <target name="test"> <echo message="compile classpath: ${compile_classpath}"/> <echo message="runtime classpath: ${runtime_classpath}"/> <echo message="test classpath: ${test_classpath}"/> <echo message="plugin classpath: ${plugin_classpath}"/> </target> </project>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <echo>Current branch: ${branch}</echo> <echo>[project.artifactId] ${project.artifactId}</echo> </target> </configuration> </execution> </executions> </plugin>
root@netkiller ~/neo (master)# mvn package -Dbranch=master [WARNING] [echo] Current branch: master [WARNING] [echo] [project.artifactId] neo
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> <inherited>false</inherited> <executions> <execution> <id>install:com.oracle:ojdbc6:11g</id> <phase>validate</phase> <goals> <goal>install-file</goal> </goals> <configuration> <file>${project.basedir}/lib/ojdbc6.jar</file> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> <packaging>jar</packaging> <createChecksum>true</createChecksum> <generatePom>true</generatePom> </configuration> </execution> </executions> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <skip>true</skip> </configuration> </plugin>
<plugin> <!--skip deploy (this is just a test module) --> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin>
mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true
mvn deploy:deploy-file \ -DrepositoryId=gitlab-maven \ -Durl=http://registry.netkiller.cn/api/v4/projects/14/packages/maven \ -Dpackaging=jar \ -Dfile=lib/cfca.jar \ -DgroupId=cn.netkiller \ -DartifactId=api \ -Dversion=1.0.0 \ -Dmaven.test.skip=true -s .ci_settings.xml
指定jar创建目录,下面配置运行 mvn package 后将在 target 目录下创建一个 project 目录。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <outputDirectory>${project.build.directory}/project</outputDirectory> </configuration> </plugin>
知道 jar 文件的默认 mainClass
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <outputDirectory>${project.build.directory}/project/lib</outputDirectory> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>cn.netkiller.web.App</mainClass> </manifest> </archive> </configuration> </plugin>
把项目依赖的包复制出来
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <!-- <version>2.10</version> --> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- 把依赖的所有maven jar包拷贝到lib目录中(这样所有的jar包都在lib目录中) --> <outputDirectory>${project.build.directory}/project/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
mvn install dependency:copy-dependencies mvn install dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/lib
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/classes/lib</outputDirectory> <includeScope>runtime</includeScope> </configuration> </execution> </executions> </plugin> </plugins> </build>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>cn.netkiller.Application</mainClass> </configuration> </plugin>
MacBook-Pro:api.netkiller.cn neo$ mvn help:describe -DgroupId=org.springframework.boot -DartifactId=spring-boot-maven-plugin -Ddetail=true [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building api 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:2.2:describe (default-cli) @ api --- [INFO] org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE Name: Spring Boot Maven Plugin Description: Spring Boot Maven Plugin Group Id: org.springframework.boot Artifact Id: spring-boot-maven-plugin Version: 1.5.6.RELEASE Goal Prefix: spring-boot This plugin has 6 goals: spring-boot:build-info Description: Generate a build-info.properties file based the content of the current MavenProject. Implementation: org.springframework.boot.maven.BuildInfoMojo Language: java Bound to phase: generate-resources Available parameters: additionalProperties Additional properties to store in the build-info.properties. Each entry is prefixed by build. in the generated build-info.properties. outputFile (Default: ${project.build.outputDirectory}/META-INF/build-info.properties) The location of the generated build-info.properties. spring-boot:help Description: Display help information on spring-boot-maven-plugin. Call mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name> to display parameter details. Implementation: org.springframework.boot.maven.HelpMojo Language: java Available parameters: detail (Default: false) User property: detail If true, display all settable properties for each goal. goal User property: goal The name of the goal for which to show help. If unspecified, all goals will be displayed. indentSize (Default: 2) User property: indentSize The number of spaces per indentation level, should be positive. lineLength (Default: 80) User property: lineLength The maximum length of a display line, should be positive. spring-boot:repackage Description: Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layout=NONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable). Implementation: org.springframework.boot.maven.RepackageMojo Language: java Bound to phase: package Available parameters: attach (Default: true) Attach the repackaged archive to be installed and deployed. classifier Classifier to add to the artifact generated. If given, the artifact will be attached with that classifier and the main artifact will be deployed as the main artifact. If this is not given (default), it will replace the main artifact and only the repackaged artifact will be deployed. Attaching the artifact allows to deploy it alongside to the original one, see the maven documentation for more details. embeddedLaunchScript The embedded launch script to prepend to the front of the jar if it is fully executable. If not specified the 'Spring Boot' default script will be used. embeddedLaunchScriptProperties Properties that should be expanded in the embedded launch script. excludeArtifactIds User property: excludeArtifactIds Comma separated list of artifact names to exclude (exact match). excludeDevtools (Default: true) Exclude Spring Boot devtools from the repackaged archive. excludeGroupIds User property: excludeGroupIds Comma separated list of groupId names to exclude (exact match). excludes Collection of artifact definitions to exclude. The Exclude element defines a groupId and artifactId mandatory properties and an optional classifier property. executable (Default: false) Make a fully executable jar for *nix machines by prepending a launch script to the jar. Currently, some tools do not accept this format so you may not always be able to use this technique. For example, jar -xf may silently fail to extract a jar or war that has been made fully-executable. It is recommended that you only enable this option if you intend to execute it directly, rather than running it with java -jar or deploying it to a servlet container. finalName (Default: ${project.build.finalName}) Required: true Name of the generated archive. includes Collection of artifact definitions to include. The Include element defines a groupId and artifactId mandatory properties and an optional classifier property. includeSystemScope (Default: false) Include system scoped dependencies. layout The type of archive (which corresponds to how the dependencies are laid out inside it). Possible values are JAR, WAR, ZIP, DIR, NONE. Defaults to a guess based on the archive type. layoutFactory The layout factory that will be used to create the executable archive if no explicit layout is set. Alternative layouts implementations can be provided by 3rd parties. mainClass The name of the main class. If not specified the first compiled class found that contains a 'main' method will be used. outputDirectory (Default: ${project.build.directory}) Required: true Directory containing the generated archive. requiresUnpack A list of the libraries that must be unpacked from fat jars in order to run. Specify each library as a <dependency> with a <groupId> and a <artifactId> and they will be unpacked at runtime. skip (Default: false) User property: skip Skip the execution. spring-boot:run Description: Run an executable archive application. Implementation: org.springframework.boot.maven.RunMojo Language: java Bound to phase: validate Before this mojo executes, it will call: Phase: 'test-compile' Available parameters: addResources (Default: false) User property: run.addResources Add maven resources to the classpath directly, this allows live in-place editing of resources. Duplicate resources are removed from target/classes to prevent them to appear twice if ClassLoader.getResources() is called. Please consider adding spring-boot-devtools to your project instead as it provides this feature and many more. agent User property: run.agent Path to agent jar. NOTE: the use of agents means that processes will be started by forking a new JVM. arguments User property: run.arguments Arguments that should be passed to the application. On command line use commas to separate multiple arguments. classesDirectory (Default: ${project.build.outputDirectory}) Required: true Directory containing the classes and resource files that should be packaged into the archive. excludeArtifactIds User property: excludeArtifactIds Comma separated list of artifact names to exclude (exact match). excludeGroupIds User property: excludeGroupIds Comma separated list of groupId names to exclude (exact match). excludes Collection of artifact definitions to exclude. The Exclude element defines a groupId and artifactId mandatory properties and an optional classifier property. folders Additional folders besides the classes directory that should be added to the classpath. fork User property: fork Flag to indicate if the run processes should be forked. fork is automatically enabled if an agent, jvmArguments or working directory are specified, or if devtools is present. includes Collection of artifact definitions to include. The Include element defines a groupId and artifactId mandatory properties and an optional classifier property. jvmArguments User property: run.jvmArguments JVM arguments that should be associated with the forked process used to run the application. On command line, make sure to wrap multiple values between quotes. NOTE: the use of JVM arguments means that processes will be started by forking a new JVM. mainClass The name of the main class. If not specified the first compiled class found that contains a 'main' method will be used. noverify User property: run.noverify Flag to say that the agent requires -noverify. profiles User property: run.profiles The spring profiles to activate. Convenience shortcut of specifying the 'spring.profiles.active' argument. On command line use commas to separate multiple profiles. skip (Default: false) User property: skip Skip the execution. useTestClasspath (Default: false) User property: useTestClasspath Flag to include the test classpath when running. workingDirectory User property: run.workingDirectory Current working directory to use for the application. If not specified, basedir will be used. NOTE: the use of working directory means that processes will be started by forking a new JVM. spring-boot:start Description: Start a spring application. Contrary to the run goal, this does not block and allows other goal to operate on the application. This goal is typically used in integration test scenario where the application is started before a test suite and stopped after. Implementation: org.springframework.boot.maven.StartMojo Language: java Bound to phase: pre-integration-test Available parameters: addResources (Default: false) User property: run.addResources Add maven resources to the classpath directly, this allows live in-place editing of resources. Duplicate resources are removed from target/classes to prevent them to appear twice if ClassLoader.getResources() is called. Please consider adding spring-boot-devtools to your project instead as it provides this feature and many more. agent User property: run.agent Path to agent jar. NOTE: the use of agents means that processes will be started by forking a new JVM. arguments User property: run.arguments Arguments that should be passed to the application. On command line use commas to separate multiple arguments. classesDirectory (Default: ${project.build.outputDirectory}) Required: true Directory containing the classes and resource files that should be packaged into the archive. excludeArtifactIds User property: excludeArtifactIds Comma separated list of artifact names to exclude (exact match). excludeGroupIds User property: excludeGroupIds Comma separated list of groupId names to exclude (exact match). excludes Collection of artifact definitions to exclude. The Exclude element defines a groupId and artifactId mandatory properties and an optional classifier property. folders Additional folders besides the classes directory that should be added to the classpath. fork User property: fork Flag to indicate if the run processes should be forked. fork is automatically enabled if an agent, jvmArguments or working directory are specified, or if devtools is present. includes Collection of artifact definitions to include. The Include element defines a groupId and artifactId mandatory properties and an optional classifier property. jmxName The JMX name of the automatically deployed MBean managing the lifecycle of the spring application. jmxPort The port to use to expose the platform MBeanServer if the application needs to be forked. jvmArguments User property: run.jvmArguments JVM arguments that should be associated with the forked process used to run the application. On command line, make sure to wrap multiple values between quotes. NOTE: the use of JVM arguments means that processes will be started by forking a new JVM. mainClass The name of the main class. If not specified the first compiled class found that contains a 'main' method will be used. maxAttempts The maximum number of attempts to check if the spring application is ready. Combined with the 'wait' argument, this gives a global timeout value (30 sec by default) noverify User property: run.noverify Flag to say that the agent requires -noverify. profiles User property: run.profiles The spring profiles to activate. Convenience shortcut of specifying the 'spring.profiles.active' argument. On command line use commas to separate multiple profiles. skip (Default: false) User property: skip Skip the execution. useTestClasspath (Default: false) User property: useTestClasspath Flag to include the test classpath when running. wait The number of milli-seconds to wait between each attempt to check if the spring application is ready. workingDirectory User property: run.workingDirectory Current working directory to use for the application. If not specified, basedir will be used. NOTE: the use of working directory means that processes will be started by forking a new JVM. spring-boot:stop Description: Stop a spring application that has been started by the 'start' goal. Typically invoked once a test suite has completed. Implementation: org.springframework.boot.maven.StopMojo Language: java Bound to phase: post-integration-test Available parameters: fork User property: fork Flag to indicate if process to stop was forked. By default, the value is inherited from the MavenProject. If it is set, it must match the value used to StartMojo start the process. jmxName The JMX name of the automatically deployed MBean managing the lifecycle of the application. jmxPort The port to use to lookup the platform MBeanServer if the application has been forked. skip (Default: false) User property: skip Skip the execution. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.976 s [INFO] Finished at: 2017-08-03T15:05:53+08:00 [INFO] Final Memory: 12M/155M [INFO] ------------------------------------------------------------------------
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat8-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8082/manager/text</url> <server>tomcat</server> <username>admin</username> <password>admin@123456</password> <path>/${project.build.finalName}</path> <!-- war文件路径缺省情况下指向target --> <!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile> --> </configuration> </plugin>
mvn tomcat:deploy
<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>cn.netkiller</groupId> <artifactId>alertmanager</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>alertmanager</name> <url>http://www.netkiller.cn</url> <description>Send SMS for Alertmanager</description> <developers> <developer> <name>Neo</name> <email>netkiller@msn.com</email> <organization>Netkiller 手札</organization> <organizationUrl>http://www.netkiller.cn</organizationUrl> <roles> <role>Author</role> </roles> </developer> </developers> <repositories> <repository> <id>alimaven</id> <name>Maven Aliyun Mirror</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> <relativePath /> </parent> <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.25</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>cn.netkiller.alertmanager.Application</mainClass> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.2</version> <configuration> <baseImage>openjdk</baseImage> <imageName>netkiller/${project.artifactId}:${project.version}</imageName> <imageTags> <imageTag>${project.version}</imageTag> <imageTag>latest</imageTag> </imageTags> <volumes>/tmp</volumes> <workdir>/app/</workdir> <!-- <cmd>["java", "-version"]</cmd> --> <entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/${project.build.finalName}.jar"]</entryPoint> <pushImage>false</pushImage> <resources> <resource> <targetPath>/app</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>