maven添加依赖
首先,如何在maven项目中添加依赖包?这个相信对于大部分人来说是非常简单的,在pom.xml文件如下配置:
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.4.3</version>
</dependency>
maven添加本地依赖
那么,如果我们想添加其他人自己开发的依赖包的话,应该如何配置呢?一般的做法是先创建一个lib文件夹,然后将依赖包拷贝进去,接着在pom.xml如下配置:
<dependency>
<groupId>com.hong.spark</groupId>
<artifactId>spark-KernelDensity_2.11</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/KernelDensity_2.11.jar</systemPath>
</dependency>
${basedir}/lib/KernelDensity_2.11.jar 这个就是你本地jar包的位置,也可以写成相对路径
项目打包
如果对于你的项目,你想要导出的jar是不包含依赖包,那就是非常简单了。直接地点击项目,然后右键选择Export
,接着选择Java--->JAR file
,最后选择你想要导出的路径就可以了。
maven打包
这里需要强调一下:maven将依赖包打包的方式有两种,
第一种是将依赖包跟项目代码一同编译然后再打包;
第二种是依赖包不与项目代码一起编译,只是将其放在一个目录下。
首先我们来看一下第一种方式如何打包吧。
将依赖包一同打进去
在你的pom.xml文件添加build配置,如以下内容:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compile.source>1.8</maven.compile.source>
<maven.compile.target>1.8</maven.compile.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${maven.compile.source}</source>
<target>${maven.compile.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.quan.simple.IncFrame</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</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>
com.quan.simple.IncFrame 这个填入的是你项目的主类。
接下来,点击项目,右键选择Run As-->Maven install
,最后就安静的等待就可以了,生成的jar包路径在你的target文件夹中。
(第一次可能需要的时间会比较长,因为需要下载很多maven的依赖包和插件)
将本地依赖包也一同打包
按上述的方法,只是将仓库依赖的jar也一起编译打在同个jar包,但是对于本地的依赖包是没有一起打进去的,那么怎么将本地依赖包也一起打进去呢?
首先,你需要新建一个assembly.xml文件,路径可以随意,然后填入以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<!-- 默认的配置 -->
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<!-- 增加scope类型为system的配置 -->
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
接着,你的pom.xml的build配置也需要进行相应的修改
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${maven.compile.source}</source>
<target>${maven.compile.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.quan.simple.IncFrame</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
其中<descriptor>assembly.xml</descriptor>
,这里要填的就是你自己的assembly.xml
的相对路径或绝对路径。
最后,跟上述方法中一样,通过maven进行打包就可以了。
可能出现的问题
可能会出现编译错误的问题,然后日志会写明:你使用的是java环境是jre而不是jdk。此时你需要将jre改为jdk。
点击项目,右键选择Properties
,找到Java Build Path
选择JRE System,点击Edit…,最后重新选为jdk就行了。
在Execution environment中选择也可以。
依赖包不与项目代码一同编译
将依赖包打包在指定的目录下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
路径可以自己设置,在这个配置参数里面:
<outputDirectory>${project.build.directory}/lib</outputDirectory>
欢迎关注同名公众号:“我就算饿死也不做程序员”。
交个朋友,一起交流,一起学习,一起进步。