为配合自动化部署hudson,最近研究了如何将eclipse maven工程打包成可运行的jar函数及对应的资源文件。
      由于我们工程中包含了多个可运行的任务,在打包成jar时需要分别导出,pom提到版本库需要一致,不能部署一个任务提交一次代码。所以考虑了在进行mvn打包时通过传递参数控制导出jar中的main所在的类。在pom中定义properties,在执行命令时传递参数即可。如mvn package -DtaskFinalName=contentpoolurl -DmainClassName=com.ifec.blueair.task.contentpoolurl.ContentPoolUrlRun,另外在通过mvn导出jar找不到配置文件,发现没有找到当前目录,通过加入manifestEntries即可指定目录。maven生成的MANIFEST.MF中的Class-Path的内容缺少一些内容,比如当前执行目录(.),那么可以通过上面manifestEntries的方式增加进来。
pom.xml内容如下:
  <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>

<parent>
        <groupId>com.ifec.blueair</groupId>
        <artifactId>blueair-packagemanager</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../blueair-packagemanager/pom.xml</relativePath>
    </parent>

<artifactId>blueair-task</artifactId>
    <packaging>jar</packaging>

<name>blueair-task</name>
    <url>http://maven.apache.org</url>

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <propertiesName>contentpoolurl</propertiesName>
        <taskFinalName>contentpoolurl</taskFinalName>
        <mainClassName>com.ifec.blueair.task.contentpoolurl.ContentPoolUrlRun</mainClassName>
    </properties>
    
    <build>
        <finalName>${taskFinalName}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.sh</include>
                    <include>**/${propertiesName}.properties</include>
                    <include>**/log4j.properties</include>
                    <include>**/config.properties</include>
                </includes>
                <filtering>true</filtering>
                <targetPath>${project.build.directory}</targetPath>  
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>${taskFinalName}_lib</classpathPrefix>
                            <mainClass>${mainClassName}</mainClass>
                        </manifest>                        <!--maven生成的MANIFEST.MF中的Class-Path的内容缺少一些内容,比如当前执行目录(.),那么可以通过上面manifestEntries的方式增加进来。-->
                        <manifestEntries>  
                            <Class-Path>.</Class-Path>  
                        </manifestEntries>  
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/${taskFinalName}_lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
    <dependencies>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>

<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>

</dependencies>
</project>

Maven 打包可运行 jar的更多相关文章

  1. spring boot maven打包可运行jar包

    普通打包之后在程序目录运行,或者编写bat运行时会提示“没有主清单属性”,这是因为并没有找到main()方法,需要我们指明告诉java程序 我bat中的代码 @echo off title mytit ...

  2. 使用Maven打包可运行jar和javaagent.jar的区别

    简介 javaagent 是 Java1.5 之后引入的新特性,其主要作用是在class被加载之前对其拦截,以插入我们的字节码. java1.5 之前使用的是JVMTI(jvm tool interf ...

  3. maven 打包可运行jar包(转)

    目录 1.前提 2.方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包 3.方法二:使用maven-assembly-plugin插件打包 4.方法三 ...

  4. 利用MAVEN打包可运行jar包,包括依赖的第三方包

    转载自:http://bglmmz.iteye.com/blog/2058914 背景: 另一篇文章说了如何利用IDEA来打包,现在来说说如何利用MAVEN打包 目标:应用本身打成一个jar包,依赖的 ...

  5. maven打包 tomcat运行pom配置 或 打成jar包

    maven打包 tomcat运行pom配置,同时还需要配置org.apache.tomcat.maven插件,这里省略. <groupId>com.company</groupId& ...

  6. 【Maven学习】Maven打包生成普通jar包、可运行jar包、包含所有依赖的jar包

    http://blog.csdn.net/u013177446/article/details/54134394 ******************************************* ...

  7. 关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题

    关于maven打包fat jar (uber jar) 时需要包含本地文件系统第三方jar文件的问题,今天折腾了一整天.最后还是用了spring boot来做.下面是几篇关于打包的有参考价值的文章,以 ...

  8. idea在maven打包时运行Test测试, 导致打包失败, 乱七八糟的错误

    在maven打包时运行Test测试, 导致打包失败, 乱七八糟的错误 在maven projects中图标toggle'skip Tests' Mode //宏杰帮助 网上案例:https://blo ...

  9. springboot(六) Maven打包引入本地jar包

       springboot Maven打包引入本地jar包 最近在做项目的时候,有一些jar包不存在maven的依赖库中,所以需要自己引入本地jar包来达到需求,那么我们该如何去将本地的jar包引入s ...

随机推荐

  1. link与@import

    导入外部样式的两种写法 <link rel="stylesheet" href="xxxx.css"> <style> @import ...

  2. 如何让你的Python程序支持多语言

    如何让你的Python程序支持多语言 本文介绍如何通过Python标准库gettext帮助你的程序支持多语言. 代码例子 import random guessesTaken = 0 print(_( ...

  3. 关于自定义Adapter实现ListView的使用

    以下为使用BaseAdapter作扩展,自定义Adapter来使用ListView控件: 需要注意以下的几点: 1.自定义Adapter时,需要特别注意Adapter类中getView()方法覆盖,注 ...

  4. json数组对象和对象数组(转)

    转自:http://www.cnblogs.com/zhangji/archive/2011/09/02/2163811.html 一.Json的简单介绍 从结构上看,所有的数据最终都可以分成三种类型 ...

  5. Python二分查找

    代码: 时间复杂度:O(log2n) #!/usr/bin/env python #coding:utf-8 import copy from copy import deepcopy ''' def ...

  6. 【JavsScript】关于javascript的路线

    Client JS: Level 1 基本对象的掌握----------------------------------------->Library(兼容)/Widget(UI+功能)---- ...

  7. 感兴趣的Linux发行版

    1. Ubuntu,包括KUbuntu,XUbuntu,Kyrin等等 2. BluestarLinux - 基于Arch Linux,感觉很美 http://sourceforge.net/proj ...

  8. Actor模型

    Actors模型(Actor model)首先是由Carl Hewitt在1973定义, 由Erlang OTP (Open Telecom Platform) 推广,其 消息传递更加符合面向对象的原 ...

  9. dnat,snat

    Iptables实现NAT是最基本的功能,大部分家用路由都是基于其SNAT方式上网,使用Iptables实现外网DNAT也很简单,不过经常会出现不能正常NAT的现象. 以下命令将客户端访问1.,很多人 ...

  10. fastjson将bean转成字符串时首字母变小写问题

    一个项目需求要求返回值为JSON格式,且大多数字段是首字母大写,还有些是类似N_TX这样的格式,在输出这样的结果时遇到了问题,由于时间紧,就直接拷贝需要的结果字段建立JavaBean类,本以为最后直接 ...