maven打包成jar⽂件与打包成⽂件
⼀、普通main⽅法⼊⼝的jar程序打包 maven-jar-plugin插件与maven-jar-plugin插件
⽤到maven-jar-plugin插件与maven-jar-plugin插件,打包完的jar⽂件所依赖包没有导包到jar⽂件中,管理不⽅便
打包命令:
maven -package
搭建过程:
1、⽬录结构:
2、l配置:
<build>
<finalName>bigdata11</finalName><!--打包完成⽂件的名字-->
<plugins>
<plugin><!--maven-jar-plugin⽤于⽣成META-INF/MANIFEST.MF⽂件的部分内容 jar包-->
<groupId>org.apache.maven.plugins</groupId> <!--引⼊maven插件的groupId-->
<artifactId>maven-jar-plugin</artifactId> <!--引⼊maven插件的artifactId-->
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath><!--MANIFEST.MF 是否增加依赖包-->
<classpathPrefix>lib/</classpathPrefix><!--MANIFEST.MF⽂件依赖包位置格式-->
maven打包本地jar包<mainClass>proxy.demo.TestMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin><!--maven-dependency-plugin插件⽤于将依赖包拷贝到<outputDirectory>${project.build.directory}/lib</outputDirectory>指定的位置,即lib⽬录下-->
<groupId>org.apache.maven.plugins</groupId><!--将依赖包放到物理存储外部 li-->
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
此⽅式只能⽣成除依赖包之外的(⾃⼰写的代码)打包成jar⽂件,会在target⽬录下⽣成jar包,并将依赖包拷贝到target/lib⽬录下,⽬录结构如下:
⼆、使⽤maven-assembly-plugin插件打包(与eclipse导出类似)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<!--依赖装配 false:指的是,把依赖包跟java程序打包成⼀个jar⽂件内;true(默认):指的是,把依赖包单独打包成⼀个jar⽂件--> <appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--此处指定main⽅法⼊⼝的class-->
<mainClass>wc.WordCountMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<!--代表执⾏插件不写executions,则打包命令变成:mvn package assembly:single-->
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包命令:
maven -package
三、maven 打包成⽂件
同样使⽤assembly插件:maven-jar-plugin maven-assembly-plugin
综述:
1、⾸先l⽂件添加插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<!-- false:⽣成的jar中,不要包含l和pom.properties这两个⽂件-->
<addMavenDescriptor>true</addMavenDescriptor>
<manifest>
<!--true:是否要把第三⽅jar放到manifest的classpath中-->
<addClasspath>true</addClasspath>
<!--⽣成的manifest中classpath的前缀,因为要把第三⽅jar放到lib⽬录下,所以classpath的前缀是lib/-->
<classpathPrefix>lib/</classpathPrefix>
<!--应⽤的main class-->
<mainClass>wc.WordCountMain</mainClass>
</manifest>
</archive>
<!--过滤掉不希望包含在jar中的⽂件-->
<excludes>
<exclude>${project.basedir}/xml/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId> maven-assembly-plugin </artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<!-- 描述⽂件路径 -->
<descriptor>src/main/l</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>wc.WordCountMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<!--名字任意 -->
<id>make-assembly</id>
<!-- 绑定到package⽣命周期阶段上 -->
<phase>package</phase>
<goals>
<!-- 只运⾏⼀次 -->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、描述⽂件(src\main\l)
<assembly>
<id>release</id><!--名字会附加在版本后⾯-->
<formats>
<format&</format><!--打包格式-->
</formats>
<!--如果为false,不会额外产⽣根⽬录,否则,在包中会产⽣以l中artifactId和version命名的根⽬录--> <includeBaseDirectory>true</includeBaseDirectory>
<!--是对依赖包的设置-->
<dependencySets>
<!--assembly中dependencySets的意思就是,将scope为runtime的依赖包,放到AlarmReport/lib⽬录下-->
<dependencySet>
<!--定义了是否解压依赖包,如果为true,会解压出依赖包中的class⽂件,反之,则不进⾏解压-->
<unpack>false</unpack>
<!--限定了对哪些依赖包进⾏操作;(依赖包scope的值是在l中定义的)-->
<scope>runtime</scope>
<!-- 依赖包在包中相对于根⽬录的路径-->
<outputDirectory>lib</outputDirectory>
<!--依赖包中是否包含当前⼯程-->
<useProjectArtifact>true</useProjectArtifact>
</dependencySet>
</dependencySets>
<!--指定哪些⽂件包含在打出的包中-->
<!--assembly中两个fileSets的作⽤是,
将target/classes下的⽂件,打包到AlarmReport⽬录;将startup.sh打包到AlarmReport/bin⽬录-->
<fileSets>
<!-- 把项⽬相关的说明⽂件,打包进zip⽂件的根⽬录 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<!-- 把项⽬的配置⽂件,打包进zip⽂件的config⽬录 -->
<fileSet>
<directory>${project.basedir}\src\main\config</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 把项⽬的脚本⽂件⽬录( src/main/scripts )中的启动脚本⽂件,打包进zip⽂件的跟⽬录 --> <fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startup.*</include>
</includes>
</fileSet>
<!-- 把项⽬的脚本⽂件(除了启动脚本⽂件),打包进zip⽂件的script⽬录 -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<exclude>startup.*</exclude>
</includes>
</fileSet>
<!-- 把项⽬⾃⼰编译出来的jar⽂件,打包进zip⽂件的根⽬录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSet> </fileSets></assembly>
打包命令:
maven -package
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论