maven常用总结
maven
scope范围
-
既然,Maven的生命周期存在编译、测试、运行这些过程,那么显然有些依赖只用于测试,比如junit;有些依赖编译用不到,只有运行的时候才能用到,比如mysql的驱动包在编译期就用不到(编译期用的是JDBC接口),而是在运行时用到的;还有些依赖,编译期要用到,而运行期不需要提供,因为有些容器已经提供了,比如servlet-api在tomcat中已经提供了,我们只需要的是编译期提供而已。
总结来说: compile:默认的scope,运行期有效,需要打入包中。 provided:编译期有效,运行期不需要提供,不会打入包中。 runtime:编译不需要,在运行期有效,需要导入包中。(接口与实现分离) test:测试需要,不会打入包中。 system:非本地仓库引入、存在系统的某个路径下的jar。(一般不使用)
打包跳过checkstyle、test阶段
-
执行命令
mvn clean package -DskipTests -Dcheckstyle.skip=true -Dcheckstyle.skipExec=true
打可运行jar包
-
在pom中新增assembly插件内容,修改main方法路径。
-
执行命令:mvn package
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.xxg.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
使用jdk1.8
-
pom中局部配置
<!-- 局部jdk配置,pom.xml中 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> -
setings.xml全局配置
<!-- 全局jdk配置,settings.xml --> <profile> <id>jdk18</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
maven添加源
-
conf/setting.xml中 mirrors 标签下新增如下配置
<mirror> <id>ALI-repository</id> <name>Team ALI jar Repository</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
maven使用本地jar
-
pom中添加以下依赖(这样操作打包时无法包含进去,暂未解决):
<!-- dependency新增 --> <dependency> <groupId>inspur</groupId> <artifactId>sdk</artifactId> <scope>system</scope> <version>1.0</version> <systemPath>${pom.basedir}/lib/inspur-1.0-SNAPSHOT-jar-with-dependencies.jar </systemPath> </dependency> <!-- 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> <compilerArguments> <extdirs>lib</extdirs> </compilerArguments> </configuration> </plugin> -
或者将本地jar上传到maven本地仓库
mvn install:install-file -Dfile=inspur-1.0-SNAPSHOT-jar-with-dependencies.jar -DgroupId=com.inspur.cloud -DartifactId=pass-sdk -Dversion=1.0 -Dpackaging=jar
maven内存溢出
-
linux下更改: /etc/profile
export MAVEN_OPTS="-Xms1024m -Xmx1024m -Xss1m" -
windows下更改:
%M2_HOME%\bin\mvn.bat 文件,添加: set MAVEN_OPTS= -Xms128m -Xmx512m 或者添加环境变量: MAVEN_OPTS的value为-Xms1024m -Xmx1024m -Xss1m

浙公网安备 33010602011771号