Maven笔记

技能树

Java开发须备技能: Java -> Maven -> JDK -> MySql -> MongoDb

maven 环境变量

vi /etc/profile

export M3_HOME=/opt/maven
export PATH=${M3_HOME}/bin:${PATH}

source /etc/profile

maven 帮助查看参数:

mvn help:describe -Dplugin=dependency -Dmojo=copy-dependencies -Dfull

打包命令

mvn8-package

export JAVA_HOME=/opt/jdk1.8.0_301
mvn clean package -Dmaven.test.skip=true -e -U -am -P nancal-snapshots -pl  $*

把它放到 /usr/bin/ 下 , 需要执行时: mvn8-package ktweb

  • 指定配置文件
    --settings c:\user\settings.xml

设置默认私服

  1. Idea 使用自定义的Maven
  2. maven setting.conf 添加:
<profiles>
    <profile>
        <id>nancal-dev</id>
        <repositories>
            <repository>
                <id>nancal-dev</id>
                <url>http://nexus.cloudnk.cn/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus</id>
                <url>http://nexus.cloudnk.cn/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>nancal-dev</activeProfile>
</activeProfiles>

Maven添加本地Jar包

 网上说了很多,只有它: http://blog.csdn.net/hhb200766/article/details/42168819 好用。
 Maven参数有几千个,与其说强大,不如说笨拙。
 添加本地lib文件夹做为仓库:

<repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>${project.basedir}/lib</url>
</repository>

添加引用:
<dependency>
    <groupId>jwd</groupId>  <!--自定义-->
    <artifactId>pzx_entity</artifactId>    <!--自定义-->
    <version>3.0.0</version> <!--自定义-->
    <!--<scope>system</scope>-->
    <!--<systemPath>${basedir}/lib/pzx_entity-3.0.0.jar</systemPath> &lt;!&ndash;项目根目录下的lib文件夹下&ndash;&gt;-->
</dependency>

lib文件夹下Jar包格式:
/groupId/artifactId/version/artifactId-verion.jar

排除包里的引用

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </exclusion>
    </exclusions>
</dependency>

scope provided

有时,一个项目中,只能有一个Jar包被引用.像:  spring-session
在 entity中引用 spring-session 时, 就要使用:  scope  provided

发布到私服

http://www.cnblogs.com/fengpingfan/p/5192738.html
http://www.cnblogs.com/winner-0715/p/7495179.html

  • 在nexus 上找到 releases , 设置 Deployment Policy -> Allow Redeploy , 同时, 点击 Summary , Copy distributionManagement 配置内容到 项目的 pom.xml 的根路径下
  • build 增加 plugin
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-source-plugin</artifactId>
	<version>3.0.1</version>
	<configuration>
		<attach>true</attach>
	</configuration>
	<executions>
		<execution>
			<id>attach-sources</id>
			<!--意思是在什么阶段打包源文件-->
			<phase>package</phase>
			<goals>
				<goal>jar-no-fork</goal>
			</goals>
		</execution>
	</executions>
</plugin>
  • 使用配置文件部署 mvn deploy --settings E:\opt\apache-maven-3.5.3\xuauto.settings.xml
  • 如果本地项目的 pom.xml 依赖父pom.xml , 手动把 父 pom.xml 上传到 nexus 的 releases 库上. nexus 会自动同步到 public 上.

Jar包瘦身

http://rickgong.iteye.com/blog/2368985

<build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <!-- 是否不包含间接依赖 -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- 忽略版本 -->
                            <stripVersion>false</stripVersion>
                            <excludeGroupIds>cn.dev8,cn.nancal</excludeGroupIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.dev8</groupId>
                            <artifactId>ktbase</artifactId>
                        </include>
                        <include>
                            <groupId>cn.dev8</groupId>
                            <artifactId>ktmyoql</artifactId>
                        </include>
                        <include>
                            <groupId>cn.dev8</groupId>
                            <artifactId>ktmvc</artifactId>
                        </include>
                        <include>
                            <groupId>cn.dev8</groupId>
                            <artifactId>ktweb</artifactId>
                        </include>
                        <include>
                            <groupId>cn.nancal</groupId>
                            <artifactId>mp-entity</artifactId>
                        </include>
                        <include>
                            <groupId>cn.nancal</groupId>
                            <artifactId>mp-orm</artifactId>
                        </include>
                    </includes>
                    <mainClass>nancal.mp.MainApplicationKt</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>

运行时, 指定 lib:
java -Dloader.path="c:/lib" -jar my-service.jar

依赖冲突

使用 Idea 插件 Maven Helper .

posted @ 2018-03-12 12:01  NewSea  阅读(218)  评论(0编辑  收藏  举报