打jar包執行main函數依賴問題

一些系統服務是經過java -jar xx.jar xx.xxx.Aaa param1 param2來啓動服務,問題就接踵而來了,
xx.jar的jar包依賴怎麼解決?java

解決方式一:
一、經過配置maven依賴,將全部依賴打成一個jar包;
該方式存在問題,若是某些依賴須要經過<scope>system</scope><systemPath>${project.basedir}/lib/neo4j-server-1.9.2-static-web.jar</systemPath>解決,
將全部依賴打成一個jar包就會有問題linux

<dependency>
    <groupId>org.neo4j.app</groupId>
    <artifactId>neo4j-server-web</artifactId>
    <version>1.9.2</version>
    
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/neo4j-server-1.9.2-static-web.jar</systemPath>
</dependency>

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
          <archive>
            <manifest>
                <mainClass>com.sniper.neo4j.util.Neo4jServerUtil</mainClass>
            </manifest>
          </archive>
      </configuration>
      <executions>
        <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
           </execution>
      </executions>
</plugin>

二、經過maven插件打包的時候設置classpath路徑,將依賴的jar包打成路徑放到MANIFEST.MF中,可是system方式的依賴一樣存在問題web

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-jar-plugin</artifactId>  
    <version>2.4</version>  
    <configuration>  
        <archive>  
            <manifest>  
                <addClasspath>true</addClasspath>  
                <classpathPrefix>lib/</classpathPrefix>  
                <mainClass>com.sniper.neo4j.util.Neo4jServerUtil</mainClass>  
            </manifest>  
        </archive>  
    </configuration>  
</plugin>

 

三、經過java -cp .;lib/* xx.xx.Aaa的方式運行就能夠解決
只要將打成的jar包、依賴的jar包都拷貝到lib文件夾下就能夠apache

注意,windows下的分割符是分號;,linux下的分隔符是冒號:windows

<!-- 打包時將依賴jar包拷貝到指定目錄 -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.8</version>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<!-- 若是不設置路徑,默認將依賴jar包導出到:target/dependency,可自定義路徑,好比:target/export -->
				<!--  
				<outputDirectory>target</outputDirectory>
				-->
				<excludeTransitive>false</excludeTransitive>
				<stripVersion>false</stripVersion>
			</configuration>
		</execution>
	</executions>
</plugin>
相關文章
相關標籤/搜索