Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
转载博客:http://quicker.iteye.com/blog/2319947
有关MAVEN仓库的理解参见:http://blog.csdn.net/wanghantong/article/details/36427433
MAVEN依赖关系中Scope的作用
- Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前依赖项的作用域<scope>可以使用5个值:
- 在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。
- compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。
- runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。
- test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。
- provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。
- system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。
- POM文件里面可以引用一些内置属性(Maven预定义可以直接使用)
- ${basedir} 项目根目录
- ${version}表示项目版本;
- ${project.basedir}同${basedir};
- ${project.version}表示项目版本,与${version}相同;
- ${project.build.directory} 构建目录,缺省为target
- ${project.build.sourceEncoding}表示主源码的编码格式;
- ${project.build.sourceDirectory}表示主源码路径;
- ${project.build.finalName}表示输出文件名称;
- ${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
如何在Maven项目中引入本地包呢?
比如我从其它项目打一个jar包,引入到现有项目中。
方法一:将待引入的包放在目录下如lib目录下,修改pom文件,加入依赖并且scope要设置为system
- <dependencies>
- <dependency>
- <groupId>com.fbcds</groupId>
- <artifactId>fbcds</artifactId>
- <version>1.0</version>
- <scope>system</scope>
- <systemPath>${project.basedir}/lib/fbcds.jar</systemPath>
- </dependency>
- </dependencies>
上面设置完成后,运行mvn package命令执行成功。但打出来的包里面不包含lib目录和fbcds.jar这个引用的包,即打出来的包不是可执行的jar。所以个人开发的话可以使用这种方式,如果团队开发请使用方法二。
方法二:将待引入的jar包安装到本地repository中
1、先把待引入的jar包放在一个目录下,需要改一下包名,如fbcds.jar修改成fbcds-1.0.jar,如F:\lib目录,在命令行CD到lib目录,执行以下命令:
- mvn install:install-file -Dfile=fbcds-1.0.jar -DgroupId=fbcds -DartifactId=fbcds -Dversion=1.0 -Dpackaging=jar
- mvn install:install-file -Dfile=ojdbc7-1.0.jar -DgroupId=ojdbc7 -DartifactId=ojdbc7 -Dversion=1.0 -Dpackaging=jar
2、修改项目pom文件加入包对应的依赖
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>fbcds</groupId>
- <artifactId>fbcds</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>ojdbc7</groupId>
- <artifactId>ojdbc7</artifactId>
- <version>1.0</version>
- </dependency>
- </dependencies>
上面的fbcds和ojdbc7就是新加的引用包的依赖。
完成后,在本地仓库可看到对应的文件夹内容:
MAVEN如何打可执行的JAR包
前提条件:已成功将待引入的jar包安装到本地repository中
方法一、使用maven-shade-plugin插件打可执行的jar包
插件查找链接:http://maven.apache.org/plugins/
1、测试类代码
- package com.lwf.test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.eclink.fbcis.store.StoreDao;
- public class TestClass {
- public static void main(String[] args) {
- StoreDao a = new StoreDao();
- System.out.println("------" + a.toString());
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- String sql = "select * from temp_head where temp_no='C530015I19008015'";
- Class.forName("oracle.jdbc.driver.OracleDriver");
- con = DriverManager.getConnection("jdbc:oracle:thin:@//10.101.2.19:1521/pdbqmytcis","qmytcis","qmytcis123");
- st = con.createStatement();
- rs = st.executeQuery(sql);
- if(rs.next()){
- System.out.println(rs.getString("temp_no"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally{
- try {
- rs.close();
- st.close();
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
上面类中引用到了fbcds和ojdbc7包的内容。
2、对应pom文件
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>222</groupId>
- <artifactId>222</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>222</name>
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>fbcds</groupId>
- <artifactId>fbcds</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>ojdbc7</groupId>
- <artifactId>ojdbc7</artifactId>
- <version>1.0</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>2.4.3</version>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>com.lwf.test.TestClass</mainClass>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:
比较两个包内容:
执行包:cmd下
original-MavenPackage-0.0.1-SNAPSHOT.jar中没有主清单属性是执行不了的。
参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/
方法二、使用maven-assembly-plugin插件打可执行的jar包
测试类与方法一中一样,只是pom不一样,pom文件如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lwf.MavenPackage</groupId>
- <artifactId>MavenPackage</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>MavenPackage</name>
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>fbcds</groupId>
- <artifactId>fbcds</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>ojdbc7</groupId>
- <artifactId>ojdbc7</artifactId>
- <version>1.0</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <!-- 使用 maven-shade-plugin插件打可执行包-->
- <!--
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>2.4.3</version>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>com.lwf.test.TestClass</mainClass>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- -->
- <!-- 使用 maven-Assembly-plugin插件打可执行包-->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <!-- get all project dependencies -->
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <!-- MainClass in mainfest make a executable jar -->
- <archive>
- <manifest>
- <mainClass>com.lwf.test.TestClass</mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
修改完pom后,在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:
两个jar文件比较:
参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
方法三、使用onejar-maven-plugin插件打可执行的jar包
测试类与方法一中一样,只是pom不一样,pom文件如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lwf.MavenPackage</groupId>
- <artifactId>MavenPackage</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>MavenPackage</name>
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>fbcds</groupId>
- <artifactId>fbcds</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>ojdbc7</groupId>
- <artifactId>ojdbc7</artifactId>
- <version>1.0</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <!-- 使用 maven-shade-plugin插件打可执行包-->
- <!--
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>2.4.3</version>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>com.lwf.test.TestClass</mainClass>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- -->
- <!-- 使用 maven-Assembly-plugin插件打可执行包-->
- <!--
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass>com.lwf.test.TestClass</mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- -->
- <!-- 使用 onejar-maven-plugin插件打可执行包-->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>com.lwf.test.TestClass</mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>com.jolira</groupId>
- <artifactId>onejar-maven-plugin</artifactId>
- <version>1.4.4</version>
- <executions>
- <execution>
- <configuration>
- <attachToBuild>true</attachToBuild>
- <classifier>onejar</classifier>
- </configuration>
- <goals>
- <goal>one-jar</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
打包截图如下:
参见:http://www.mkyong.com/maven/maven-create-a-fat-jar-file-one-jar-example/
上文中因googlecode中已没有onejar-maven-plugin所以另请参见下文:
http://my.oschina.net/noahxiao/blog/78241
方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下
其他不变,pom文件如下
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lwf.MavenPackage</groupId>
- <artifactId>MavenPackage</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>MavenPackage</name>
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>fbcds</groupId>
- <artifactId>fbcds</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>ojdbc7</groupId>
- <artifactId>ojdbc7</artifactId>
- <version>1.0</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <!-- 方法一:使用 maven-shade-plugin插件打可执行包-->
- <!--
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>2.4.3</version>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>com.lwf.test.TestClass</mainClass>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- -->
- <!-- 方法二:使用 maven-Assembly-plugin插件打可执行包-->
- <!--
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass>com.lwf.test.TestClass</mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- -->
- <!-- 方法三:使用 onejar-maven-plugin插件打可执行包-->
- <!--
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>com.lwf.test.TestClass</mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>com.jolira</groupId>
- <artifactId>onejar-maven-plugin</artifactId>
- <version>1.4.4</version>
- <executions>
- <execution>
- <configuration>
- <attachToBuild>true</attachToBuild>
- <classifier>onejar</classifier>
- </configuration>
- <goals>
- <goal>one-jar</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- -->
- <!-- 方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <excludes>
- <exclude>**/log4j.properties</exclude>
- </excludes>
- <archive>
- <manifest>
- <addClasspath>true</addClasspath>
- <mainClass>com.lwf.test.TestClass</mainClass>
- <classpathPrefix>lib/</classpathPrefix>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <!-- Copy project dependency -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <version>2.5.1</version>
- <executions>
- <execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <!-- exclude junit, we need runtime dependency only -->
- <includeScope>runtime</includeScope>
- <outputDirectory>${project.build.directory}/lib/</outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
可以看到依赖的包拷贝到了lib目录下,打的包里没有依赖包的信息,只是简单的包,不过Manifest文件class-path要包含引用名的路径
- Manifest-Version: 1.0
- Built-By: lweifeng
- Build-Jdk: 1.7.0_17
- Class-Path: lib/log4j-1.2.17.jar lib/fbcds-1.0.jar lib/ojdbc7-1.0.jar
- Created-By: Apache Maven 3.3.9
- Main-Class: com.lwf.test.TestClass
- Archiver-Version: Plexus Archiver
在以上前三种插件打包方式中,maven-shade-plugin和maven-assembly-plugin采取的是将依赖包解压再一并打到新包中,这样依赖包可能存在冲突的时候,导致运行时可能出现未知问题,而onejar-maven-plugin打包是将依赖包自动归入lib目录,不解压原包,相当于在原包基础上加壳,这样可以避免冲突的发生。第四种方法即是我们原来ant打包所使用的方法。
- MavenPackage.zip (5.2 KB)
- 下载次数: 17
转载于:https://www.cnblogs.com/albert-think/p/7387965.html
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包相关推荐
- Maven 生成打包可执行jar包
文章目录 1. 需求 2. 开发环境 3. Maven打包插件介绍 4. Maven使用maven-jar-plugin打可执行jar包 5. Maven使用maven-assembly-plugin ...
- java 执行jar包命令
不依赖外部jar包 执行java -cp xxx.jar xxx.xxxx.xxxx.MainClass 参数一,参数二 依赖外部jar包 当main类依赖多个jar时,可以把多个jar打包到一个目录 ...
- Maven仓库理解和优先级
前言 使用 maven 也有一段时间了,有时候在配置 repository,mirror,profile的时候,总会导致 jar 拉取不到.所以认真的分析了 maven 获取 jar 包时候的优先级. ...
- 使用Maven 实现打包生成一个可执行jar包:附详细配置解释说明
1. 需求 项目打包,满足以下要求: 1.整个项目打一个Zip包下面包括应用程序.应用程序依赖的jar包.说明文档 2.项目打的jar包可以执行不同类里的Main函数 3.项目源码打的jar包要与依赖 ...
- maven上传Jar包到私服及从私服下载jar包相关配置
有关使用Maven常见问题总结(Eclipse中使用Maven.Maven项目部署到tomcat等问题) http://blog.csdn.net/sunitjy/article/details/42 ...
- maven install后,java -jar XXXX.jar运行---找不到主类问题 以及 虚拟机中执行jar包后 访问页面出现Java heap space等其他问题
这是前几天遇到的问题了,当天晚上想写下来来着,后来有事情就一直搁置到现在了. 由于我想将SpringCloud项目都导出jar包在虚拟机上运行,然后本地访问,所以先将SpringCloud中的注册中心 ...
- Maven生成可以直接运行的jar包的多种方式
Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...
- 【收藏】Maven 生成打包可执行jar包
Maven 生成打包可执行jar包 https://blog.csdn.net/daerzei/article/details/82883472
- maven导出Java方法_eclipse导出maven工程的可执行jar包
一.eclipse导出maven工程的可执行jar包 建立两个maven工程 ZKServer 和ZKClient 注意:用maven进行开发管理的话,默认的打出来的jar包是不能运行的,需要在pom ...
最新文章
- extern “C“
- P1552-[APIO2012]派遣【左偏树】
- linux获取笔记本摄像头视频,如何在windows下用ffmpeg抓取笔记本电脑摄像头视频
- python正则表达式去除逗号_[宜配屋]听图阁 - python正则表达式去掉数字中的逗号(python正则匹配逗号)...
- abp 上如何使用getstoredproccommand_多效唑,如何在柑橘上安全使用
- 从事前端开发,日常工作中必备的工具有哪些?
- [转]项目中常用的19条MySQL优化
- 已解决:不小心卸载pip后(重新安装pip的两种方式)
- 生日快乐程序_7天获客6万,小程序助力品牌对抗“行业寒冬”
- 二重积分的复化Simpson方法
- 470万条12306用户信息遭泄露
- 这里告诉大家,“后端开发工程师”到底是做什么的。
- sht30使用心得自述
- Tensorflow LMST预测股票
- TCP-IP协议详解(5) 我尽力(IP协议详解)
- 利用七参数进行CGCS2000坐标系到西安80坐标系的转换
- CSS学习中十条速记口诀
- 中国市场迎来AR-HUD元年
- 2k14无法打开因为计算机,2k14闪退怎么办 2k14闪退解决方法
- JAVA基础复习笔记——day2