maven
config
1 | macos |
command
- mvn compile 编译,将Java 源程序编译成 class 字节码文件。
- mvn test 测试,并生成测试报告
- mvn clean 将以前编译得到的旧的 class 字节码文件删除
- mvn pakage 打包,动态 web工程打 war包,Java工程打 jar 包。
- mvn install 将项目生成 jar 包放在仓库中,以便别的模块调用
- Maven的几个常用plugin
other
1 | package |
scope
compile
默认就是compile,什么都不配置也就是意味着compile。compile表示被依赖项目需要参与当前项目的编译,当然后续的测试,运行周期也参与其中,是一个比较强的依赖。打包的时候通常需要包含进去。test
scope为test表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译,执行。比较典型的如junit。runtime
runntime表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过编译而已,说实话在终端的项目(非开源,企业内部系统)中,和compile区别不是很大。比较常见的如JSR×××的实现,对应的API jar是compile的,具体实现是runtime的,compile只需要知道接口就足够了。oracle jdbc驱动架包就是一个很好的例子,一般scope为runntime。另外runntime的依赖通常和optional搭配使用,optional为true。我可以用A实现,也可以用B实现。provided
provided意味着打包的时候可以不用包进去,别的设施(Web Container)会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是在打包阶段做了exclude的动作。system
从参与度来说,和provided相同,不过被依赖项不会从maven仓库抓,而是从本地文件系统拿,一定需要配合systemPath属性使用
solution
多项目之间相互引用
应该在父目录下进行maven install操作,会自动生成子模块的jar或war包。
解决maven无法加载本地lib/下的jar包问题(程序包XXX不存在)
原因
若该程序包是第三方的jar,解决方案是让maven既加载maven库中的jar包,又要加载本地WEB-INF/lib下的jar包。
解决
1
2
3
4
5
6
7
8
9
10
11
12
13<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
创建本地引用包
- 直接引用本地jar
将jar放在项目中,例如web项目就放在 webapp/WEB-INF/lib下面
然后再pom.xml中添加jar的依赖:
1 | <dependency> |
- 安装到本地仓库
1 | mvn install:install-file -Dfile=xxx.jar -DgroupId=xx.xxx.xx -DartifactId=xx -Dversion=xx -Dpackaging=jar |
依赖传递原则
几乎所有的Jar包冲突都和依赖传递原则有关,所以我们先说Maven中的依赖传递原则:
最短路径优先原则
假如引入了2个Jar包A和B,都传递依赖了Z这个Jar包:
A -> X -> Y -> Z(2.5)
B -> X -> Z(2.0)
那其实最终生效的是Z(2.0)这个版本。因为他的路径更加短。如果我本地引用了Z(3.0)的包,那生效的就是3.0的版本。一样的道理。
最先声明优先原则
如果路径长短一样,优先选最先声明的那个。
A -> Z(3.0)
B -> Z(2.5)
这里A最先声明,所以传递过来的Z选择用3.0版本的。
add git info into package
1 | <plugin> |
生成的
git.properties会在jar包位置BOOT-INF/classes/git.properties
publish
remote repo
step1: config /path/to/maven/conf/settings.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<settings>
<servers>
<server>
<id>central</id>
<username>xxx</username>
<password>xxx</password>
</server>
</servers>
......
<profiles>
<profile>
<id>gpg-key</id>
<properties>
<gpg.keyname>xxx</gpg.keyname>
<gpg.passphrase>xxx</gpg.passphrase>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>gpg-key</activeProfile>
</activeProfiles>
</settings>需要保证keyid对应的secret key在服务器上已存在,如不存在,需要进行import
gpg –armor –export-secret-keys xxx > secret-key.asc # 原有机器
gpg –import secret-key.asc # 导入机器
检验: gpg –list-keys –keyid-format LONG
注意:如果是普通用户导入secret key,需要在tmux中进行导入,因为非tmux环境 The terminal device (TTY, e.g.,
/dev/pts/X) is not owned by the user running GPG. Check withls -l $(tty),因为只用当前拥有TTY权限,才可以This ensures GPG agent can access the terminal and has permission to generate the key.step2: publish
1
mvn clean deploy -DskipTests # -Pgpg-key
local repo
troubleshooting
由于没有mvn install操作,没有在本地下载依赖包到lib目录下,所以启动项目,会提示java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener错误
gradle
command
1 | Upgrade Gradle Version in a Java Project |
daemon
1 | check daemons |
task
1 | list tasks |
build
1 | download dependency |
publish
plugin: com.vanniktech.maven.publish
remote repo
1 | offical |
config secret
optional 1: gradle.properties
1
2
3
4
5
6mavenCentralUsername=username
mavenCentralPassword=the_password
=12345678
=some_password
=/Users/yourusername/.gnupg/secring.gpgoptional 2: env
1
2
3
4
5
6
7
8
9ORG_GRADLE_PROJECT_mavenCentralUsername=username
ORG_GRADLE_PROJECT_mavenCentralPassword=the_password
short key id
ORG_GRADLE_PROJECT_signingInMemoryKey=exported_ascii_armored_key
Optional
ORG_GRADLE_PROJECT_signingInMemoryKeyId=12345678
If key was created with a password.
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword=some_passwordtip
1
2
3signing.keyId 是取8位 short keyid,
the 8-character short form of PGP key ID. For example, if full key ID is 633B1F30DF975148, the short form would be DF975148.
signing.keyId=DF975148
local repo
1 | ./gradlew publishToMavenLocal |
build.gradle
1 | mavenPublishing { |