利用maven插件实现java项目双击执行


在实际项目开发过程中,有时候我们需要开发一个服务(比如webservice服务),或者通过java程序实现了某个特定的需要定期频繁操作的功能,那么我们可以通过maven插件,来打包项目,同时配置相关启动项,实现双击即可执行java项目的目的,从而可以快捷操作java项目。

引入pom插件及第三方jar包

配置文件如下所示:

<build>
		
		<finalName>${project.artifactId}</finalName>
		<plugins>	
			<!-- 满足maven打包成可执行文件的插件配置 [begin]-->
		
			<!-- maven打jar插件 -->
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/assembly/dist.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
			 <!-- 满足maven打包成可执行文件的插件配置 [end]-->  
		</plugins>
         
	</build>

创建MANIFEST.MF文件

在pom文件中【manifestFile】标签描述了需要打成的jar包需要引入的第三方jar的MANIFEST.MF文件路径。MANIFEST.MF文件内容格式如下:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: gurj
Build-Jdk: 1.6.0_38
Specification-Title: Unnamed - net.bwda:InformServer:jar:0.0.1-SNAPSHOT
Specification-Version: 0.0.1-SNAPSHOT
Specification-Vendor: xxxxx公司
Implementation-Title: Unnamed - net.bwda:InformServer:jar:0.0.1-SNAPSHOT
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: net.bwda
Implementation-Vendor: xxxxx公司
Main-Class: com.bwda.sdk.ServicePublisher
Class-Path: activation-1.1.jar
  activation-1.1.jar
  animal-sniffer-annotations-1.14.jar
  apache-mime4j-core-0.7.2.jar
  axiom-1.2.20.pom
  axiom-api-1.2.20.jar
  axiom-impl-1.2.20.jar
  axis2-1.6.2.jar
  axis2-kernel-1.7.7.jar
  axis2-transport-http-1.7.7.jar
  axis2-transport-local-1.7.7.jar
  bcprov-jdk15on-1.54.jar
  bwda-remote-0.0.1-SNAPSHOT.jar
  byte-buddy-1.8.15-.jar
  checker-compat-qual-2.0.0.jar
  commons-beanutils-1.8.0.jar
  commons-codec-1.10.jar
  commons-collections-3.2.1.jar
  commons-exec-1.3-.jar
  commons-fileupload-1.3.3.jar
  commons-httpclient-3.1.jar
  commons-io-2.1.jar
  commons-lang-2.5.jar
  commons-lang3-3.8.1.jar
  commons-logging-1.2.jar
  commons-net-3.6.jar
  db2jcc-1.0.jar
  dom4j-1.6.1.jar
  error_prone_annotations-2.1.3.jar
  ers-schema-2.3.0-298.jar
  ers-sdk-2.3.0-298.jar
  ezmorph-1.0.6.jar
  fastjson-1.2.47.jar
  geronimo-activation_1.1_spec-1.1.jar
  geronimo-jta_1.1_spec-1.1.jar
  geronimo-stax-api_1.0_spec-1.0.1.jar
  geronimo-ws-metadata_2.0_spec-1.1.2.jar
  gson-2.8.2.jar
  guava-25.0-jre-.jar
  httpclient-4.5.6.jar
  httpcore-4.4.10.jar
  interfaceSDK-0.0.1-SNAPSHOT.jar
  j2objc-annotations-1.1.jar
  jackson-core-asl-1.9.2.jar
  jackson-jaxrs-1.9.2.jar
  jackson-mapper-asl-1.9.2.jar
  jackson-xc-1.9.2.jar
  javax.mail-api-1.5.6.jar
  jaxb-api-2.2.2.jar
  jaxb-impl-2.2.3-1.jar
  jaxen-1.1.6.jar
  jersey-client-1.17.1.jar
  jersey-core-1.17.1.jar
  jersey-json-1.18.jar
  jettison-1.1.jar
  jsch-0.1.55.jar
  json-lib-2.4-jdk15.jar
  jsr305-1.3.9.jar
  jsr311-api-1.1.1.jar
  junit-4.4.jar
  log4j-1.2.16.jar
  mysql-connector-java-5.1.10.jar
  neethi-3.1.1.jar
  ojdbc14-10.2.0.4.0.jar
  okhttp-3.11.0-.jar
  okio-1.14.0-.jar
  orion-ssh2-214.jar
  oro-2.0.8.jar
  poi-3.6.jar
  poi-ooxml-3.6.jar
  poi-ooxml-schemas-3.6.jar
  postgresql-9.4.1208.jre6.jar
  selenium-api-3.141.59-.jar
  selenium-chrome-driver-3.141.59-.jar
  selenium-edge-driver-3.141.59-.jar
  selenium-firefox-driver-3.141.59.jar
  selenium-ie-driver-3.141.59-.jar
  selenium-java-3.141.59.jar
  selenium-opera-driver-3.141.59-.jar
  selenium-remote-driver-3.141.59-.jar
  selenium-safari-driver-3.141.59-.jar
  selenium-support-3.141.59-.jar
  servlet-api-2.3.jar
  sqljdbc4-16.2.jar
  stax-api-1.0-2.jar
  stax-api-1.0.1.jar
  stax2-api-3.1.1.jar
  swis-client-0.0.1-SNAPSHOT.jar
  velocity-1.6.4.jar
  woden-core-1.0M10.jar
  woodstox-core-asl-4.2.0.jar
  wsdl4j-1.6.3.jar
  xml-apis-1.0.b2.jar
  xmlbeans-2.3.0.jar
  xmlschema-core-2.2.3.jar
  ganymed-ssh2-262-sources.jar
  sunjce_provider.jar
  ../conf/
Company: com.bwda

上述文件中Main-Class指定了程序启动时,执行的main方法所在类路径。
Class-Path指定了打包后所引用的第三方jar包,目前我都是一个个全部写下来了,我总感觉应该可以写一个类似路径或别的什么配置,而不必一个个都写下来,因为这样的话每次新增加一个jar,都要修改下上述文件,很是繁琐,然而在网上搜索资料,暂时还没找到替换的方案,如哪位大神知道怎么搞,请赐教,不胜感激。

创建dist.xml文件

在pom文件中maven-assembly-plugin插件需要配置dist.xml文件,此文件我的理解是配置对于打好的jar包,所需要的各种资源,比如配置文件、启动项等。
文件内容如下所示:

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
	<id>dist</id>
	<formats>
		<format>tar.gz</format>
	</formats>
	<baseDirectory>interfaceSDK</baseDirectory>
	<includeBaseDirectory>true</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>archive/bin/</directory>
			<outputDirectory>/bin</outputDirectory>
			<lineEnding>unix</lineEnding>
			<includes>
				<include>*.sh</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>archive/bin/</directory>
			<outputDirectory>/bin</outputDirectory>
			<includes>
				<include>*.bat</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>archive/conf/</directory>
			<outputDirectory>/conf</outputDirectory>
			<lineEnding>unix</lineEnding>
			<includes>
				<include>*</include>
			</includes>
		</fileSet>		
		<!-- 是否打包资源目录下的test类
		<fileSet>
            <directory>${project.build.directory}/test-classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>-->
	</fileSets>
	<dependencySets>
		<dependencySet>
			<useProjectArtifact>true</useProjectArtifact>
			<outputDirectory>/lib</outputDirectory>			
            <!-- <scope>test</scope> -->
		</dependencySet>
	</dependencySets>
</assembly>

按照配置描述,添加对应的文件

主要就是添加MANIFEST.MF文件和dist.xml文件
我的项目中,文件存储位置如下所示:
在这里插入图片描述
在这里插入图片描述

添加可执行文件

主要是在windows环境下的bat文件和linux环境下的sh文件
bat文件如下所示:

@echo off
rem -------------------------------------------------------------------------
rem InformServer Bootstrap Script for Win32
rem -------------------------------------------------------------------------

rem $Id: InformServer.bat 001 2016-03-14 17:40:26Z xxx@xxx.xxx $

title xxxxx

pushd %DIRNAME%..
set APP_HOME=%CD%
popd

rem Java virtual machine variables defined ================================== 

set MAIN_JAR_NAME=../lib/interfaceSDK-0.0.1-SNAPSHOT.jar

set JAVA_OPTS='-Xms2048m -Xmx2048m'

rem =========================================================================


set "RUNJAR=%APP_HOME%/%MAIN_JAR_NAME%"
SET "CLASSPATH=%CLASSPATH%;%APP_HOME%/conf"

if not "%JAVA_HOME%" == "" goto HAVE_JAVA_HOME

set JAVA=java

echo JAVA_HOME is not set.  Unexpected results may occur.
echo Set JAVA_HOME to the directory of your local JDK to avoid this message.
goto end

:HAVE_JAVA_HOME

set JAVA=%JAVA_HOME%\bin\java

echo ===============================================================================
echo.
echo   Authenticate Bootstrap Environment
echo.
echo ===============================================================================
echo.

"%JAVA%" -jar "%MAIN_JAR_NAME%" "%JAVA_OPTS%"

:end
pause

上述文件主要修改的地方是:
【MAIN_JAR_NAME】配置项
sh文件如下所示:

#!/bin/bash

### ====================================================================== ###
##                                                                          ##
##  xxxxxx Script                                       ##
##                                                                          ##
### ====================================================================== ###

### $Id: xxxx.sh 2016-03-14 17:41:41Z  $ ###

DIRNAME=`dirname $0`
APP_HOME=`cd $DIRNAME/..; pwd`

# Setup the JVM
if [ "x$JAVA_HOME" != "x" ]; then
	if [ ! -d "$JAVA_HOME" ]; then
		echo "$JAVA_HOME is invalid!"
		exit 1
	else
	    JAVA="$JAVA_HOME/bin/java"
	fi
else
	echo "Cann't find JAVA_HOME configuration!"
	exit 1
fi

if [ "x$JAVA" = "x" ]; then
    if [ "x$JAVA_HOME" != "x" ]; then
		JAVA="$JAVA_HOME/bin/java"
    else
		JAVA="$JAVA_HOME/bin/java"
    fi
fi



AUTH_CLASSPATH=$AUTH_CLASSPATH:$APP_HOME/conf
MAINJAR=$APP_HOME/lib/interfaceSDK-0.0.1-SNAPSHOT.jar

JAVA_OPTS="-Xms2048m -Xmx2048m"

# main method
$JAVA -jar $MAINJAR $JAVA_OPTS

上述文件主要修改的地方是:
【MAINJAR】配置项
上述文件编辑好后,放到项目中,我这边存放的目录为:
在这里插入图片描述

将配置文件存放在指定目录下

由于在步骤三中的dist.xml文件,重新制定了项目所引用的配置文件,因此需要将相关配置文件在复制一份,存放在指定目录下。我这边存放的目录如下所示:
在这里插入图片描述

执行maven打包命令

在这里插入图片描述

查看打包文件及启动测试

将打包好的xxxx.tar.gz文件解压缩,里面会看到lib、conf、bin文件夹,其中lib里存放的是项目所引用的第三方jar包,conf里存放的是配置文件,bin里存放的是可执行文件。
进入到bin文件夹里,执行对应的bat或ssh命令,即可实现双击直接运行的效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值