知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
http://maven.apache.org/
$ sudo apt-get install maven2 $ mvn -version Apache Maven 2.2.1 (rdebian-4) Java version: 1.6.0_22 Java home: /usr/lib/jvm/java-6-openjdk/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux" version: "2.6.38-8-generic" arch: "amd64" Family: "unix"
JAVA_HOME="/usr/lib/jvm/java-6-openjdk/jre" MAVEN_HOME="/usr/share/maven2/"
多綫程下載 -Dmaven.artifact.threads=10 預設是 5
mvn -Dmaven.artifact.threads=10 test 您可以使用MAVEN_OPTS環境變數。例如: export MAVEN_OPTS = -Dmaven.artifact.threads = 10
創建 Maven 項目
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.company -DartifactId=my-app
mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp
編譯項目的原始碼
$ mvn compile
編譯測試的原始碼
mvn test-compile
運行測試
$ mvn test
打包但不測試
mvn -D maven.test.skip=true package
忽略測試失敗
mvn test -Dmaven.test.failure.ignore
將編譯後的代碼打包
$ mvn package
$ ls target/*.war target/www.netkiller.cn-0.0.1-SNAPSHOT.war
防止出現臟數據,可以先 clean 然後再打包,同時為了加快打包速度,逃過測試
mvn clean package -Dmaven.test.skip=true
將項目打包後安裝到本地倉庫,可以作為其它項目的本地依賴。
$ mvn install
跳過測試
mvn install -Dmaven.test.skip=true
$ mvn dependency:build-classpath [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building api.netkiller.cn 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.10:build-classpath (default-cli) @ api.cf88.com --- [INFO] Dependencies classpath: /www/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.3.0.RELEASE/spring-boot-starter-web-1.3.0.RELEASE.jar:......:/www/.m2/repository/junit/junit/4.12/junit-4.12.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.692 s [INFO] Finished at: 2016-08-10T17:32:49+08:00 [INFO] Final Memory: 25M/162M [INFO] ------------------------------------------------------------------------
[www@iZ62yln3rjjZ repository]$ mvn dependency:tree [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building api.example.com 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ api.cf88.com --- [INFO] cf88.com:api.cf88.com:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.3.0.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.3.0.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.0.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.3:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.3:compile [INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.13:compile [INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.13:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.16:runtime [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.0.RELEASE:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.28:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.28:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.28:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.28:compile [INFO] | +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.0.RELEASE:compile [INFO] | | \- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile [INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.1.0:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.3:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.3:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.6.3:compile [INFO] | +- org.springframework:spring-web:jar:4.2.3.RELEASE:compile [INFO] | | \- org.springframework:spring-aop:jar:4.2.3.RELEASE:compile [INFO] | | \- aopalliance:aopalliance:jar:1.0:compile [INFO] | \- org.springframework:spring-webmvc:jar:4.2.3.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.0.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:1.3.0.RELEASE:compile [INFO] | | \- org.aspectj:aspectjweaver:jar:1.8.7:compile [INFO] | +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile [INFO] | | +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile [INFO] | | | +- antlr:antlr:jar:2.7.7:compile [INFO] | | | \- org.jboss:jandex:jar:1.1.0.Final:compile [INFO] | | +- dom4j:dom4j:jar:1.6.1:compile [INFO] | | | \- xml-apis:xml-apis:jar:1.0.b2:compile [INFO] | | +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile [INFO] | | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile [INFO] | | \- org.javassist:javassist:jar:3.18.1-GA:compile [INFO] | +- javax.transaction:javax.transaction-api:jar:1.2:compile [INFO] | +- org.springframework.data:spring-data-jpa:jar:1.9.1.RELEASE:compile [INFO] | | \- org.springframework:spring-orm:jar:4.2.3.RELEASE:compile [INFO] | \- org.springframework:spring-aspects:jar:4.2.3.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.0.RELEASE:compile [INFO] | +- org.apache.tomcat:tomcat-jdbc:jar:8.0.28:compile [INFO] | | \- org.apache.tomcat:tomcat-juli:jar:8.0.28:compile [INFO] | \- org.springframework:spring-jdbc:jar:4.2.3.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-redis:jar:1.3.0.RELEASE:compile [INFO] | +- org.springframework.data:spring-data-redis:jar:1.6.1.RELEASE:compile [INFO] | | \- org.springframework:spring-context-support:jar:4.2.3.RELEASE:compile [INFO] | \- redis.clients:jedis:jar:2.7.3:compile [INFO] | \- org.apache.commons:commons-pool2:jar:2.4.2:compile [INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:1.3.0.RELEASE:compile [INFO] | \- org.mongodb:mongo-java-driver:jar:2.13.3:compile [INFO] +- org.springframework.boot:spring-boot-starter-amqp:jar:1.3.0.RELEASE:compile [INFO] | +- org.springframework:spring-messaging:jar:4.2.3.RELEASE:compile [INFO] | \- org.springframework.amqp:spring-rabbit:jar:1.5.2.RELEASE:compile [INFO] | +- org.springframework.retry:spring-retry:jar:1.1.2.RELEASE:compile [INFO] | +- org.springframework.amqp:spring-amqp:jar:1.5.2.RELEASE:compile [INFO] | +- com.rabbitmq:http-client:jar:1.0.0.RELEASE:compile [INFO] | | \- org.apache.httpcomponents:httpclient:jar:4.5.1:compile [INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile [INFO] | | \- commons-codec:commons-codec:jar:1.9:compile [INFO] | \- com.rabbitmq:amqp-client:jar:3.5.6:compile [INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.3.0.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:1.3.0.RELEASE:compile [INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.0.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.3.0.RELEASE:test [INFO] | +- org.mockito:mockito-core:jar:1.10.19:test [INFO] | | \- org.objenesis:objenesis:jar:2.1:test [INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test [INFO] | +- org.springframework:spring-core:jar:4.2.3.RELEASE:compile [INFO] | \- org.springframework:spring-test:jar:4.2.3.RELEASE:test [INFO] +- org.springframework.data:spring-data-mongodb:jar:1.8.1.RELEASE:compile [INFO] | +- org.springframework:spring-tx:jar:4.2.3.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:4.2.3.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:4.2.3.RELEASE:compile [INFO] | +- org.springframework:spring-expression:jar:4.2.3.RELEASE:compile [INFO] | +- org.springframework.data:spring-data-commons:jar:1.11.1.RELEASE:compile [INFO] | +- org.slf4j:slf4j-api:jar:1.7.13:compile [INFO] | \- org.slf4j:jcl-over-slf4j:jar:1.7.13:compile [INFO] +- mysql:mysql-connector-java:jar:5.1.37:compile [INFO] +- com.google.code.gson:gson:jar:2.3.1:compile [INFO] \- junit:junit:jar:4.12:test [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.400 s [INFO] Finished at: 2016-08-09T10:25:07+08:00 [INFO] Final Memory: 23M/163M [INFO] ------------------------------------------------------------------------
$ mvn dependency:copy-dependencies
$ ls target/dependency/ antlr-2.7.7.jar javassist-3.18.1-GA.jar snakeyaml-1.16.jar spring-boot-starter-web-1.3.0.RELEASE.jar spring-webmvc-4.2.3.RELEASE.jar aopalliance-1.0.jar javax.transaction-api-1.2.jar spring-aop-4.2.3.RELEASE.jar spring-boot-starter-websocket-1.3.0.RELEASE.jar spring-websocket-4.2.3.RELEASE.jar aspectjweaver-1.8.7.jar jboss-logging-3.3.0.Final.jar spring-aspects-4.2.3.RELEASE.jar spring-context-4.2.3.RELEASE.jar thymeleaf-2.1.4.RELEASE.jar classmate-1.1.0.jar jboss-logging-annotations-1.2.0.Beta1.jar spring-beans-4.2.3.RELEASE.jar spring-core-4.2.3.RELEASE.jar thymeleaf-layout-dialect-1.3.1.jar dom4j-1.6.1.jar jcl-over-slf4j-1.7.13.jar spring-boot-1.3.0.RELEASE.jar spring-data-commons-1.11.1.RELEASE.jar thymeleaf-spring4-2.1.4.RELEASE.jar groovy-2.4.4.jar jstl-1.2.jar spring-boot-autoconfigure-1.3.0.RELEASE.jar spring-data-jpa-1.9.1.RELEASE.jar tomcat-embed-core-8.0.28.jar hibernate-commons-annotations-4.0.5.Final.jar jul-to-slf4j-1.7.13.jar spring-boot-starter-1.3.0.RELEASE.jar spring-expression-4.2.3.RELEASE.jar tomcat-embed-el-8.0.28.jar hibernate-core-4.3.11.Final.jar log4j-over-slf4j-1.7.13.jar spring-boot-starter-aop-1.3.0.RELEASE.jar spring-jdbc-4.2.3.RELEASE.jar tomcat-embed-logging-juli-8.0.28.jar hibernate-entitymanager-4.3.11.Final.jar logback-classic-1.1.3.jar spring-boot-starter-data-jpa-1.3.0.RELEASE.jar spring-messaging-4.2.3.RELEASE.jar tomcat-embed-websocket-8.0.28.jar hibernate-jpa-2.1-api-1.0.0.Final.jar logback-core-1.1.3.jar spring-boot-starter-jdbc-1.3.0.RELEASE.jar spring-orm-4.2.3.RELEASE.jar tomcat-jdbc-8.0.28.jar hibernate-validator-5.2.2.Final.jar mybatis-3.3.0.jar spring-boot-starter-logging-1.3.0.RELEASE.jar spring-security-config-4.0.3.RELEASE.jar tomcat-juli-8.0.28.jar jackson-annotations-2.6.3.jar mybatis-spring-1.2.3.jar spring-boot-starter-security-1.3.0.RELEASE.jar spring-security-core-4.0.3.RELEASE.jar unbescape-1.1.0.RELEASE.jar jackson-core-2.6.3.jar mysql-connector-java-5.1.37.jar spring-boot-starter-thymeleaf-1.3.0.RELEASE.jar spring-security-web-4.0.3.RELEASE.jar validation-api-1.1.0.Final.jar jackson-databind-2.6.3.jar ognl-3.0.8.jar spring-boot-starter-tomcat-1.3.0.RELEASE.jar spring-tx-4.2.3.RELEASE.jar xml-apis-1.0.b2.jar jandex-1.1.0.Final.jar slf4j-api-1.7.13.jar spring-boot-starter-validation-1.3.0.RELEASE.jar spring-web-4.2.3.RELEASE.jar
--encrypt-master-password
neo@MacBook-Pro ~ % mvn --encrypt-master-password test {LhCRW9y8CG4HfT7ExHI3msP56Cv+fgjdiNhTk883hZs=}
顯示插件的詳細信息
MacBook-Pro:deployment neo$ mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-war-plugin -Ddetail=true [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom --- [INFO] org.apache.maven.plugins:maven-war-plugin:3.1.0 Name: Apache Maven WAR Plugin Description: Builds a Web Application Archive (WAR) file from the project output and its dependencies. Group Id: org.apache.maven.plugins Artifact Id: maven-war-plugin Version: 3.1.0 Goal Prefix: war This plugin has 4 goals: war:exploded Description: Create an exploded webapp in a specified directory. Implementation: org.apache.maven.plugins.war.WarExplodedMojo Language: java Bound to phase: package Available parameters: archive The archive configuration to use. See Maven Archiver Reference. archiveClasses (Default: false) Whether a JAR file will be created for the classes in the webapp. Using this optional configuration parameter will make the compiled classes to be archived into a JAR file and the classes directory will then be excluded from the webapp. cacheFile (Default: ${project.build.directory}/war/work/webapp-cache.xml) Required: true The file containing the webapp structure cache. containerConfigXML The path to a configuration file for the servlet container. Note that the file name may be different for different servlet containers. Apache Tomcat uses a configuration file named context.xml. The file will be copied to the META-INF directory. delimiters Set of delimiters for expressions to filter within the resources. These delimiters are specified in the form 'beginToken*endToken'. If no '*' is given, the delimiter is assumed to be the same for start and end. So, the default filtering delimiters might be specified as: <delimiters> <delimiter>${*}</delimiter> <delimiter>@</delimiter> </delimiters> Since the '@' delimiter is the same on both ends, we don't need to specify '@*@' (though we can). dependentWarExcludes The comma separated list of tokens to exclude when doing a WAR overlay. Default is Overlay.DEFAULT_EXCLUDES dependentWarIncludes The comma separated list of tokens to include when doing a WAR overlay. Default is Overlay.DEFAULT_INCLUDES escapedBackslashesInFilePath (Default: false) To escape interpolated values with Windows path c:\foo\bar will be replaced with c:\\foo\\bar. escapeString Expression preceded with this String won't be interpolated. \${foo} will be replaced with ${foo}. filteringDeploymentDescriptors (Default: false) To filter deployment descriptors. Disabled by default. filters Filters (property files) to include during the interpolation of the pom.xml. includeEmptyDirectories (Default: false) (no description available) nonFilteredFileExtensions A list of file extensions that should not be filtered. Will be used when filtering webResources and overlays. outputFileNameMapping The file name mapping to use when copying libraries and TLDs. If no file mapping is set (default) the files are copied with their standard names. overlays The overlays to apply. Each <overlay> element may contain: - id (defaults to currentBuild) - groupId (if this and artifactId are null, then the current project is treated as its own overlay) - artifactId (see above) - classifier - type - includes (a list of string patterns) - excludes (a list of string patterns) - filtered (defaults to false) - skip (defaults to false) - targetPath (defaults to root of webapp structure) recompressZippedFiles (Default: true) Indicates if zip archives (jar,zip etc) being added to the war should be compressed again. Compressing again can result in smaller archive size, but gives noticeably longer execution time. resourceEncoding (Default: ${project.build.sourceEncoding}) The encoding to use when copying filtered web resources. supportMultiLineFiltering (Default: false) Stop searching endToken at the end of line useCache (Default: false) Whether the cache should be used to save the status of the webapp across multiple runs. Experimental feature so disabled by default. useDefaultDelimiters (Default: true) Use default delimiters in addition to custom delimiters, if any. useJvmChmod (Default: true) use jvmChmod rather that cli chmod and forking process warSourceDirectory (Default: ${basedir}/src/main/webapp) Required: true Single directory for extra files to include in the WAR. This is where you place your JSP files. warSourceExcludes The comma separated list of tokens to exclude when copying the content of the warSourceDirectory. warSourceIncludes (Default: **) The comma separated list of tokens to include when copying the content of the warSourceDirectory. webappDirectory (Default: ${project.build.directory}/${project.build.finalName}) Required: true The directory where the webapp is built. webResources The list of webResources we want to transfer. webXml The path to the web.xml file to use. workDirectory (Default: ${project.build.directory}/war/work) Required: true Directory to unpack dependent WARs into if needed. war:help Description: Display help information on maven-war-plugin. Call mvn war:help -Ddetail=true -Dgoal=<goal-name> to display parameter details. Implementation: org.apache.maven.plugins.war.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. war:inplace Description: Generate the webapp in the WAR source directory. Implementation: org.apache.maven.plugins.war.WarInPlaceMojo Language: java Available parameters: archive The archive configuration to use. See Maven Archiver Reference. archiveClasses (Default: false) Whether a JAR file will be created for the classes in the webapp. Using this optional configuration parameter will make the compiled classes to be archived into a JAR file and the classes directory will then be excluded from the webapp. cacheFile (Default: ${project.build.directory}/war/work/webapp-cache.xml) Required: true The file containing the webapp structure cache. containerConfigXML The path to a configuration file for the servlet container. Note that the file name may be different for different servlet containers. Apache Tomcat uses a configuration file named context.xml. The file will be copied to the META-INF directory. delimiters Set of delimiters for expressions to filter within the resources. These delimiters are specified in the form 'beginToken*endToken'. If no '*' is given, the delimiter is assumed to be the same for start and end. So, the default filtering delimiters might be specified as: <delimiters> <delimiter>${*}</delimiter> <delimiter>@</delimiter> </delimiters> Since the '@' delimiter is the same on both ends, we don't need to specify '@*@' (though we can). dependentWarExcludes The comma separated list of tokens to exclude when doing a WAR overlay. Default is Overlay.DEFAULT_EXCLUDES dependentWarIncludes The comma separated list of tokens to include when doing a WAR overlay. Default is Overlay.DEFAULT_INCLUDES escapedBackslashesInFilePath (Default: false) To escape interpolated values with Windows path c:\foo\bar will be replaced with c:\\foo\\bar. escapeString Expression preceded with this String won't be interpolated. \${foo} will be replaced with ${foo}. filteringDeploymentDescriptors (Default: false) To filter deployment descriptors. Disabled by default. filters Filters (property files) to include during the interpolation of the pom.xml. includeEmptyDirectories (Default: false) (no description available) nonFilteredFileExtensions A list of file extensions that should not be filtered. Will be used when filtering webResources and overlays. outputFileNameMapping The file name mapping to use when copying libraries and TLDs. If no file mapping is set (default) the files are copied with their standard names. overlays The overlays to apply. Each <overlay> element may contain: - id (defaults to currentBuild) - groupId (if this and artifactId are null, then the current project is treated as its own overlay) - artifactId (see above) - classifier - type - includes (a list of string patterns) - excludes (a list of string patterns) - filtered (defaults to false) - skip (defaults to false) - targetPath (defaults to root of webapp structure) recompressZippedFiles (Default: true) Indicates if zip archives (jar,zip etc) being added to the war should be compressed again. Compressing again can result in smaller archive size, but gives noticeably longer execution time. resourceEncoding (Default: ${project.build.sourceEncoding}) The encoding to use when copying filtered web resources. supportMultiLineFiltering (Default: false) Stop searching endToken at the end of line useCache (Default: false) Whether the cache should be used to save the status of the webapp across multiple runs. Experimental feature so disabled by default. useDefaultDelimiters (Default: true) Use default delimiters in addition to custom delimiters, if any. useJvmChmod (Default: true) use jvmChmod rather that cli chmod and forking process warSourceDirectory (Default: ${basedir}/src/main/webapp) Required: true Single directory for extra files to include in the WAR. This is where you place your JSP files. warSourceExcludes The comma separated list of tokens to exclude when copying the content of the warSourceDirectory. warSourceIncludes (Default: **) The comma separated list of tokens to include when copying the content of the warSourceDirectory. webappDirectory (Default: ${project.build.directory}/${project.build.finalName}) Required: true The directory where the webapp is built. webResources The list of webResources we want to transfer. webXml The path to the web.xml file to use. workDirectory (Default: ${project.build.directory}/war/work) Required: true Directory to unpack dependent WARs into if needed. war:war Description: Build a WAR file. Implementation: org.apache.maven.plugins.war.WarMojo Language: java Bound to phase: package Available parameters: archive The archive configuration to use. See Maven Archiver Reference. archiveClasses (Default: false) Whether a JAR file will be created for the classes in the webapp. Using this optional configuration parameter will make the compiled classes to be archived into a JAR file and the classes directory will then be excluded from the webapp. attachClasses (Default: false) Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the project as an additional artifact. By default the classifier for the additional artifact is 'classes'. You can change it with the someclassifier parameter. If this parameter true, another project can depend on the classes by writing something like: myGroup myArtifact myVersion classes cacheFile (Default: ${project.build.directory}/war/work/webapp-cache.xml) Required: true The file containing the webapp structure cache. classesClassifier (Default: classes) The classifier to use for the attached classes artifact. classifier Classifier to add to the generated WAR. If given, the artifact will be an attachment instead. The classifier will not be applied to the JAR file of the project - only to the WAR file. containerConfigXML The path to a configuration file for the servlet container. Note that the file name may be different for different servlet containers. Apache Tomcat uses a configuration file named context.xml. The file will be copied to the META-INF directory. delimiters Set of delimiters for expressions to filter within the resources. These delimiters are specified in the form 'beginToken*endToken'. If no '*' is given, the delimiter is assumed to be the same for start and end. So, the default filtering delimiters might be specified as: Since the '@' delimiter is the same on both ends, we don't need to specify '@*@' (though we can). dependentWarExcludes The comma separated list of tokens to exclude when doing a WAR overlay. Default is Overlay.DEFAULT_EXCLUDES dependentWarIncludes The comma separated list of tokens to include when doing a WAR overlay. Default is Overlay.DEFAULT_INCLUDES escapedBackslashesInFilePath (Default: false) To escape interpolated values with Windows path c:\foo\bar will be replaced with c:\\foo\\bar. escapeString Expression preceded with this String won't be interpolated. \${foo} will be replaced with ${foo}. failOnMissingWebXml Whether or not to fail the build if the web.xml file is missing. Set to false if you want your WAR built without a web.xml file. This may be useful if you are building an overlay that has no web.xml file. Starting with 3.1.0, this property defaults to false if the project depends on the Servlet 3.0 API or newer. filteringDeploymentDescriptors (Default: false) To filter deployment descriptors. Disabled by default. filters Filters (property files) to include during the interpolation of the pom.xml. includeEmptyDirectories (Default: false) (no description available) nonFilteredFileExtensions A list of file extensions that should not be filtered. Will be used when filtering webResources and overlays. outputDirectory (Default: ${project.build.directory}) Required: true The directory for the generated WAR. outputFileNameMapping The file name mapping to use when copying libraries and TLDs. If no file mapping is set (default) the files are copied with their standard names. overlays The overlays to apply. Each overlay element may contain: - id (defaults to currentBuild) - groupId (if this and artifactId are null, then the current project is treated as its own overlay) - artifactId (see above) - classifier - type - includes (a list of string patterns) - excludes (a list of string patterns) - filtered (defaults to false) - skip (defaults to false) - targetPath (defaults to root of webapp structure) packagingExcludes The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. Hint: read the about (?!Pattern). packagingIncludes The comma separated list of tokens to include in the WAR before packaging. By default everything is included. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. primaryArtifact (Default: true) Whether this is the main artifact being built. Set to false if you don't want to install or deploy it to the local repository instead of the default one in an execution. recompressZippedFiles (Default: true) Indicates if zip archives (jar,zip etc) being added to the war should be compressed again. Compressing again can result in smaller archive size, but gives noticeably longer execution time. resourceEncoding (Default: ${project.build.sourceEncoding}) The encoding to use when copying filtered web resources. skip (Default: false) User property: maven.war.skip You can skip the execution of the plugin if you need to. Its use is NOT RECOMMENDED, but quite convenient on occasion. supportMultiLineFiltering (Default: false) Stop searching endToken at the end of line useCache (Default: false) Whether the cache should be used to save the status of the webapp across multiple runs. Experimental feature so disabled by default. useDefaultDelimiters (Default: true) Use default delimiters in addition to custom delimiters, if any. useJvmChmod (Default: true) use jvmChmod rather that cli chmod and forking process warSourceDirectory (Default: ${basedir}/src/main/webapp) Required: true Single directory for extra files to include in the WAR. This is where you place your JSP files. warSourceExcludes The comma separated list of tokens to exclude when copying the content of the warSourceDirectory. warSourceIncludes (Default: **) The comma separated list of tokens to include when copying the content of the warSourceDirectory. webappDirectory (Default: ${project.build.directory}/${project.build.finalName}) Required: true The directory where the webapp is built. webResources The list of webResources we want to transfer. webXml The path to the web.xml file to use. workDirectory (Default: ${project.build.directory}/war/work) Required: true Directory to unpack dependent WARs into if needed. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.960 s [INFO] Finished at: 2017-08-03T14:33:18+08:00 [INFO] Final Memory: 10M/155M [INFO] ------------------------------------------------------------------------
MacBook-Pro:api.netkiller.cn neo$ mvn help:describe \ -DgroupId=org.springframework.boot \ -DartifactId=spring-boot-maven-plugin \ -Ddetail=true
定義 properties
<properties> <spring.version>4.0.1.RELEASE</spring.version> </properties>
引用 properties
<!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency>
例 2.1. Maven properties
pom.xml 檔案
<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> <groupId>com.javahash.web</groupId> <artifactId>Spring4MVCHelloWorld</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Spring4MVCHelloWorld Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.0.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>Spring4MVCHelloWorld</finalName> </build> </project>
<repository> <id>mvnrepository</id> <name>mvnrepository</name> <url>http://www.mvnrepository.com/</url> <layout>default</layout> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.24.1</version> </dependency> </dependencies>
聲明父項目引用包的版本號。
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.3.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.3.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
resources 用來處理 src/main/resources 目錄中得內容
<resources> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/conf</targetPath> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>development.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>development.properties</include> </includes> <targetPath>resources</targetPath> </resource> <resource> <directory>lib</directory> <includes> <include>**/*.sh</include> <include>**/*.bat</include> </includes> <targetPath>${project.build.directory}/lib</targetPath> </resource> </resources>
將資源檔案編譯後複製到 WEB-INF/classes 目錄中
<resources> <resource> <directory>src/resources</directory> </resource> </resources>
include / exclude
<resources> <!-- Filter jdbc.properties & mail.properties. NOTE: We don't filter applicationContext-infrastructure.xml, let it go with spring's resource process mechanism. --> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>jdbc.properties</include> <include>mail.properties</include> </includes> </resource> <!-- Include other files as resources files. --> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>jdbc.properties</exclude> <exclude>mail.properties</exclude> </excludes> </resource> </resources>
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> [...] </project>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>cn.netkiller.Oracle</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
對於大型互聯網項目,不可能把所有代碼都放在一個項目目錄下,常常會將一個項目拆分成多個子項目
例如一個電商系統
在開發過程中常常會遇到這種需求,有一個部分代碼是共用的,所有項目都會使用到。所以需要將這部分代碼獨立成一個項目。
測試目錄深度
Project | |--- common -> https://example.com/xxxx/common.git | | ---pom.xml |--- project1 | |--- pom.xml |--- project2 | |--- pom.xml |---pom.xml
common 是公共項目,獨立倉庫。通過git submodule 技術掛載到項目目錄。project1,project2 構建依賴 common 項目產生的 jar 包。
例 2.2. Maven parent
<?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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>maven</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit.jupiter.version>5.4.0</junit.jupiter.version> </properties> <dependencies> </dependencies> <modules> <module>project1</module> <module>project2</module> <module>common</module> </modules> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> </plugins> </build> </project>
注意
<packaging>pom</packaging> 必須是 pom
項目下面的子模組
<modules> <module>project1</module> <module>project2</module> <module>common</module> </modules>
例 2.3. watir-webdriver example
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>demo</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>common</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> </project>
添加 parent 標籤, 聲明項目的父子關係
<parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>demo</groupId> <artifactId>project1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>project1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>demo</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
聲明項目的父子關係
<parent> <groupId>demo</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
由於 project1 依賴 common 加入下面依賴
<dependency> <groupId>demo</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
project2 跟 project1 類似
在父項目目錄運行 mvn package
neo@MacBook-Pro ~/workspace/maven % mvn package [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] maven [pom] [INFO] common [jar] [INFO] project1 [jar] [INFO] project2 [jar] [INFO] [INFO] -----------------------------< demo:maven >----------------------------- [INFO] Building maven 0.0.1-SNAPSHOT [1/4] [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] ----------------------------< demo:common >----------------------------- [INFO] Building common 0.0.1-SNAPSHOT [2/4] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ common --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/common/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ common --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ common --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/common/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ common --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ common --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] [INFO] Results: [INFO] [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ common --- [INFO] Building jar: /Users/neo/workspace/maven/common/target/common-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/demo/common/pom.xml already added, skipping [INFO] META-INF/maven/demo/common/pom.properties already added, skipping [INFO] [INFO] ---------------------------< demo:project1 >---------------------------- [INFO] Building project1 0.0.1-SNAPSHOT [3/4] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ project1 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project1/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ project1 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project1 --- [INFO] Building jar: /Users/neo/workspace/maven/project1/target/project1-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/demo/project1/pom.xml already added, skipping [INFO] META-INF/maven/demo/project1/pom.properties already added, skipping [INFO] [INFO] ---------------------------< demo:project2 >---------------------------- [INFO] Building project2 0.0.1-SNAPSHOT [4/4] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project2 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project2/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ project2 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project2 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project2/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project2 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project2/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ project2 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project2 --- [INFO] Building jar: /Users/neo/workspace/maven/project2/target/project2-0.0.1-SNAPSHOT.jar [INFO] META-INF/maven/demo/project2/pom.xml already added, skipping [INFO] META-INF/maven/demo/project2/pom.properties already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for maven 0.0.1-SNAPSHOT: [INFO] [INFO] maven .............................................. SUCCESS [ 0.006 s] [INFO] common ............................................. SUCCESS [ 2.317 s] [INFO] project1 ........................................... SUCCESS [ 0.539 s] [INFO] project2 ........................................... SUCCESS [ 0.101 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.115 s [INFO] Finished at: 2019-02-28T17:03:29+08:00 [INFO] ------------------------------------------------------------------------ neo@MacBook-Pro ~/workspace/maven %
我們可以看到有三個包產生
neo@MacBook-Pro ~/workspace/maven % find . -iname '*.jar' ./project1/target/project1-0.0.1-SNAPSHOT.jar ./common/target/common-0.0.1-SNAPSHOT.jar ./project2/target/project2-0.0.1-SNAPSHOT.jar
我們也可以單獨編譯子項目
neo@MacBook-Pro ~/workspace/maven/project1 % mvn package [INFO] Scanning for projects... [INFO] [INFO] ---------------------------< demo:project1 >---------------------------- [INFO] Building project1 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ project1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project1/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project1 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/neo/workspace/maven/project1/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project1 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/neo/workspace/maven/project1/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ project1 --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project1 --- [INFO] Building jar: /Users/neo/workspace/maven/project1/target/project1-0.0.1-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.015 s [INFO] Finished at: 2019-02-28T17:09:18+08:00 [INFO] ------------------------------------------------------------------------
共享庫 common-0.0.1-SNAPSHOT.jar 已經安裝到 ~/.m2 目錄下。
neo@MacBook-Pro ~/workspace/maven/project1 % find ~/.m2 -iname 'common-0.0.1-SNAPSHOT.jar' /Users/neo/.m2/repository/demo/common/0.0.1-SNAPSHOT/common-0.0.1-SNAPSHOT.jar
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.netkiller</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.netkiller</groupId> <artifactId>dependencies</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>dependencies</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.netkiller</groupId> <version>0.0.1-SNAPSHOT</version> <artifactId>parent</artifactId> </parent> <artifactId>example</artifactId> <name>example</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>cn.netkiller</groupId> <artifactId>dependencies</artifactId> <version>${project.parent.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> </dependencies> </project>
配置預設的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定義
or alternatively, we can use an external build.xml. <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-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>
指定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>
<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