开始的话:
好久没有接触java了,最近有了解图像数据库orientdb 3.0.x由于只有java的接口,所以我在上手数据库的时候,不得不在来温习一下。
知道java社区一直很活跃,但看了才知道,java开发的IDE也有了更多的选择而不仅仅是eclipse了,java也有自己的项目管理工具maven了,我已经与java的世界脱节了。
我在网上看了很多讲解maven的上手的blog, 也写得很好,但是不够简明扼要。我做个简单的操作笔记。
maven上手:
下载:
可以在官网寻找最新板本,也可以直接apache-maven-3.6.0-bin.tar.gz,3.6.0是目前(2019-02-28)最新的一个版本
环境搭建:
maven本身是一个java程序程序,所以有跨平台的运行的自带光环,要解决的就是运行方式,在官方包中提供了一个bash脚本和一个cmd脚本, 基本能解决大多数兼容问题。具体环境配置比较基础,这里就不在赘述。
环境配置:
主要是本地库的地址配置和远程厂库地址,如果嫌麻烦,可以下载一个替换, 下面是我修改的个简单笔记。
... 省略 ...
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>/var/maven_repo</localRepository>
... 省略 ...
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name> Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>nexus-osc</id>
<mirrorOf>central</mirrorOf>
<name>Nexus osc</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
<mirror>
<id>ui</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://uk.maven.org/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
</mirrors>
... 省略 ...
使用方法:
其实用maven也简单, 我总结出一个步骤,大概是4个命令一个修改。
$ mvn archetype:generate -DgroupId=com.company -DartifactId=project_name #创建项目
创建项目后, 有一个 project_name同名的目录。进入目录中,会发现一个pom.xml的文件,这个就是maven在当前目录的项目管理文件。里面有依赖和编译打包等操作的配置。
其实本身也很简单,比如我要添加对orientdb-client的依赖。只需要在dependencies节点中加入像下面格式的子节点:
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-client</artifactId>
<version>3.0.0</version>
</dependency>
在pom.xml中添加编译配置
<build>
<plugins>
<plugin>
<!--plugins要写在build标签下,不然无法识别 -->
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<!--maven-compiler-plugin版本写一个中央仓库有的版本,保存后它会自动下载 -->
<configuration>
<source>10</source>
<!--我的jdk是10.0.1的,写成10.0.1不好用,所以就写成10了 -->
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
$ mvn clean install -U #更新本地库,如果不更新,每次编译都会在中心厂库下载依赖库
$ mvn clean package #编译,这里会生成一个jar,但是不能直接用java -jar 运行
$ mvn exec:java -Dexec.mainClass="com.company.App" # 运行
打包:
运行的时候会发现,每次运行要等一会其他的balabala的东西,才能调用到我们的源码。 有没有其他方法来解决这个问题呢。当然有,可以看看打包jar的 Maven打包三种方法(推荐第三种)博客,看着就有点麻烦,我就用mvn命令将就运行一下了。
恩恩,大概就是怎么多了,才接触maven工具,有什么不对的地方,欢迎留言指出。