安装与配置
首先,确保jdk已安装并配置好。
其次,下载apache maven, 地址 https://maven.apache.org/download.cgi ,因为是linux,所以下载 tar.gz版本。
下载完成后,解压,将文件夹移动到/usr/local
下,在 /etc/profile 中做如下配置(类似jdk配置,~/.bashrc
里配置应该也可以,不过这里就不做尝试了):
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=${PATH}:${MAVEN_HOME}/bin
要注意,里面不要有中文字符,否则配置将不起作用,最好手打,从其他参考拷贝过来的话,很有可能会是中文字符。
当以下的命令使得能够显示maven版本之后,安装配置成功。
mvn -v
修改本地仓库
/usr/local/apache-**/conf/setting.xml
中设置,不设置时使用默认仓库${user.home}/.m2/repository
。
<!-- 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>/lib</localRepository>
添加本地依赖
示例。
<dependency>
<groupId>cas.shao.cglt</groupId>
<artifactId>tornado</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/3rdParty/tornado.jar</systemPath>
</dependency>
关于Eclipse中 Maven-Update Project JRE会变为1.5的处理方法
只对本项目有效。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
也可直接在配置文件中修改,见解决maven update project 后项目jdk变成1.5的问题。
关于报错 package not match的处理方法
Build Path - Configure Build Path… - 在Source选项中选择所有文件夹并选择右侧Remove - 右侧Add Folder, 选择 main/java 和 main/resources。
关于添加maven-dependency-plugin后报错的处理方法
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-dependency-plugin:3.6.2 or one of its dependencies could not be resolved: Failure to find org.apache.maven.plugins:maven-dependency-plugin:jar:3.6.2 in https://repo.maven.apache.org/mav..
安装的Maven版本为3.6, 但是 maven-dependency-plugin 没有这么高的版本,将版本降到2.8或者2.9之类的,更新工程即可。
关于Mybatis Mapper代理找不到对应xml配置文件的处理方法
Mapper 的java文件和xml文件须在一个目录下,所以程序打包的时候需要将xml资源也打包在内否则会找不到xml文件。
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
本地jar安装至本地仓库
mvn install:install-file -Dfile=***.jar -DgroupId=*.*.* -DartifactId=*** -Dversion=*.* -Dpackaging=jar
安装完成会提示Build Success, 可检查本地仓库中是否有这个目录,目录结构为上面定义的DgroupId/DartifactId。
maven-assembly-plugin
maven-assembly-plugin 可以满足打包需求,但是如果需要将自己的jar或者第三方jar打包进去时,这个插件是不认 放在本地目录中的jar的,即使是pom中已经添加了本地依赖,也必须得将自己的jar或者第三方jar 安装到本地仓库中才行。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- <addClasspath>true</addClasspath> -->
<mainClass>cas.shao.cglt.*.*</mainClass>
</manifest>
</archive>
<!-- <descriptors>
<descriptor>src/main/resources/descriptor.xml</descriptor>
</descriptors> -->
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
<!-- 加上下面这一段,使用 mvn package命令,不加则使用mvn assembly-->
<executions>
<execution>
<id>make-assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
因为没有什么其他特殊的要求,所以这里直接使用了jar-with-dependencies
。
将生成的可执行jar拷贝到用户目录
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-jar</id>
<phase>package</phase>
<configuration>
<tasks>
<copy todir="${user.home}">
<fileset dir="${project.build.directory}">
<include name="${project.artifactId}-${project.version}-jar-with-dependencies.jar" />
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>