<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # IDEA打包 打包自定義工具類文件 ![](https://box.kancloud.cn/941c100dbbd1c9d035c83b49a25a3377_508x409.png) ![](https://box.kancloud.cn/a2658365b9298b8ca481aeaa73cdf105_686x299.png) ![](https://box.kancloud.cn/664a44d035d77cb34d108ba022bab302_515x396.png) ![](https://box.kancloud.cn/d4f14c90ae4b4cd4594a5edc4e29ac0d_797x425.png) ![](https://box.kancloud.cn/dd8c3d461bfe70941a890932a8a930a2_433x493.png) ![](https://box.kancloud.cn/dc6591d676a357cb5f94fdeb555d8e8e_304x231.png) 然后`java -jar xx.jar` # maven war包 注意:需要在pom.xml中注明打包方式為war ![](https://box.kancloud.cn/951fc21a4107679fc69bed865eed0a8d_766x376.png) 點擊界面最右側的選項:Maven Projects -> 雙擊package ![](https://box.kancloud.cn/fd7ec77c31be029414a6450596d892b0_224x333.png) war包存在目錄結構 ![](https://box.kancloud.cn/30dfb2a3543b3764778cf34cdcb00cc1_342x382.png) 測試 `java -jar xx.jar` 測試成功!(該war包內含tomcat) # maven-assembly-plugin 在pom.xml中加入一個插件來打包,如下: ~~~ <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.yj.spark.TestSpark</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ~~~ 這種方式是通過maven命令來打包的: ![](https://box.kancloud.cn/4861060748b9c8a42cb4b7ab695992f9_168x354.png) 或者 `mvn assembly:assembly` 見名知意,其中 spark-1.0.jar 只有代碼,無依賴,而 spark-1.0-jar-with-dependencies.jar則包含所有jar包 ![](https://box.kancloud.cn/b4c90825a2f045fc7f3b2831795a8687_292x235.png) # 詳解 簡單的說,maven-assembly-plugin 就是用來幫助打包用的,比如說打出一個什么類型的包,包里包括哪些內容等等。 目前至少支持以下打包類型: * zip * tar * tar.gz * tar.bz2 * jar * dir * war 默認情況下,打jar包時,只有在類路徑上的文件資源會被打包到jar中,并且文件名是${artifactId}-${version}.jar,下面看看怎么用maven-assembly-plugin插件來定制化打包 首先需要添加插件聲明: ~~~ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ~~~ 默認情況下,maven-assembly-plugin內置了幾個可以用的assembly descriptor: * bin : 類似于默認打包,會將bin目錄下的文件打到包中 * jar-with-dependencies : 會將所有依賴都解壓打包到生成物中 * src :只將源碼目錄下的文件打包 * project : 將整個project資源打包 使用 descriptors,指定打包文件 src/assembly/src.xml,在該配置文件內指定打包操作。 ~~~ <project> [...] <build> [...] <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <descriptors> <descriptor>src/assembly/src.xml</descriptor> </descriptors> </configuration> [...] </project> ~~~ 要查看它們的詳細定義,可以到maven-assembly-plugin-2.4.jar里去看,例如對應 bin 的assembly descriptor 如下: ~~~ <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>bin</id> <formats> <format>tar.gz</format> <format>tar.bz2</format> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}</directory> <outputDirectory>/</outputDirectory> <includes> <include>README*</include> <include>LICENSE*</include> <include>NOTICE*</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}/site</directory> <outputDirectory>docs</outputDirectory> </fileSet> </fileSets> </assembly> ~~~ ## 描述符文件元素 ~~~ <id>release</id> ~~~ id 標識符,添加到生成文件名稱的后綴符。如果指定 id 的話,目標文件則是 `${artifactId}-${id}.tar.gz` ## formats maven-assembly-plugin 支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同時指定多個打包格式 ~~~ <formats> <format>tar.gz</format> <format>dir</format> </formats> ~~~ ## dependencySets 用來定制工程依賴 jar 包的打包方式,核心元素如下表所示 ![](https://box.kancloud.cn/3fbf442aee6624c20097de6ca0ba1b09_643x150.png) ~~~ <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> </dependencySet> </dependencySets> ~~~ ## fileSets 管理一組文件的存放位置,核心元素如下表所示。 ![](https://box.kancloud.cn/a08aa5e0037818a863eed27948c6047f_792x190.png) ~~~ <fileSets> <fileSet> <includes> <include>bin/**</include> </includes> <fileMode>0755</fileMode> </fileSet> <fileSet> <includes> <include>/conf/**</include> <include>logs</include> </includes> </fileSet> </fileSets> ~~~ ## files 可以指定目的文件名到指定目錄,其他和 fileSets 相同,核心元素如下表所示 ![](https://box.kancloud.cn/414f5c391aadee1a801f7059a2d19571_580x185.png) ~~~ <files> <file> <source>README.txt</source> <outputDirectory>/</outputDirectory> </file> </files> ~~~ 例子: pom.xml ~~~ <build> <finalName>scribe-log4j2-test</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptors> <descriptor>src/main/assembly/release.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ~~~ release.xml ~~~ <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <formats> <format>tar.gz</format> <format>dir</format> </formats> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> </dependencySet> </dependencySets> <fileSets> <fileSet> <includes> <include>bin/**</include> </includes> <fileMode>0755</fileMode> </fileSet> <fileSet> <includes> <include>/conf/**</include> <include>logs</include> </includes> </fileSet> </fileSets> <files> <file> <source>README.txt</source> <outputDirectory>/</outputDirectory> </file> </files> </assembly> ~~~ ## 自定義Assembly Descriptor ~~~ <?xml version='1.0' encoding='UTF-8'?> <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>demo</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly> ~~~ 這個定義很簡單: * format:指定打包類型 * includeBaseDirectory:指定是否包含打包層目錄(比如finalName是output,當值為true,所有文件被放在output目錄下,否則直接放在包的根目錄下) * fileSets:指定要包含的文件集,可以定義多個fileSet * directory:指定要包含的目錄 * outputDirectory:指定當前要包含的目錄的目的地 要使用這個assembly descriptor,需要如下配置: ~~~ <configuration> <finalName>demo</finalName> <descriptors> <descriptor>assemblies/demo.xml</descriptor> </descriptors> <outputDirectory>output</outputDirectory> </configuration> ~~~ 最后會生成一個demo-demo.jar 文件在目錄 output 下,其中前一個demo來自finalName,后一個demo來自assembly descriptor中的id,其中的內容和默認的打包出來的jar類似。 如果只想有finalName,則增加配置: ~~~ <appendAssemblyId>false</appendAssemblyId> ~~~ **添加文件** 上面演示了添加所有編譯后的資源,同樣的可以增加其他資源,例如想添加當前工程目錄下的某個文件 b.txt ,在assembly descriptor的assembly結點下增加 ~~~ <files> <file> <source>b.txt</source> <outputDirectory>/</outputDirectory> </file> </files> ~~~ 這里用到了 files 元素類型,可以想象 fileSets 下的結點都是針對文件夾的;files 下的結點都是針對文件的。 也可以改變打包后的文件名,例如上面的 b.txt ,希望打包后的名字為 b.txt.bak, 只需要在file 里添加以下配置 : ~~~ <destName>b.txt.bak</destName> ~~~ **排除文件** 在fileSet里可以使用includes 和 excludes來更精確的控制哪些文件要添加,哪些文件要排除。 例如要排除某個目錄下所有的txt文件: ~~~ <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> <excludes> <exclude>**/*.txt</exclude> </excludes> </fileSet> ~~~ 或者某個目錄下只想 .class 文件: ~~~ <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> <includes> <include>**/*.class</include> </includes> </fileSet> ~~~ **添加依賴** 如果想把一些依賴庫打到包里,可以用 dependencySets 元素,例如最簡單的,把當前工程的所有依賴都添加到包里: ~~~ <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> </dependencySet> </dependencySets> ~~~ 如果要排除工程自身生成的jar,則可以添加: ~~~ <useProjectArtifact>false</useProjectArtifact> ~~~ unpack參數可以控制依賴包是否在打包進來時是否解開,例如解開所有包,添加以下配置 ~~~ <unpack>true</unpack> ~~~ 和 fileSet 一樣,可以使用 excludes 和 includes 來更詳細的控制哪些依賴需要打包進來;另外 useProjectAttachments,useTransitiveDependencies,useTransitiveFiltering等參數可以對間接依賴、傳遞依賴進行控制 ## 其他選項 * moduleSets:當有子模塊時候用 * repositories:想包含庫的時候用 * containerDescriptorHandlers:可以進行一些合并,定義ArtifactHandler之類的時候可以用 * componentDescriptors:如上所述,可以包含一些componentDescriptor定義,這些定義可以被多個assembly共享 ## Assembly Plugin更多配置 **指定Main-Class** archive的一個重要用處就是配置生成的MANIFEST.MF文件。默認會生成一個MANIFEST.MF文件,不過這個文件默認值沒什么意義。如果想指定生成jar的Main-Class,可以如下配置: ~~~ <archive> <manifest> <mainClass>demo.DemoMain</mainClass> </manifest> </archive> ~~~ **添加MANIFEST項** 除了可以指定Main-Class外,還可以添加任意項。比如在OSGI bundle的MANIFEST.MF定義里就有很多用來定義bundle的屬性的項,如Import-Package,Export-Package等等。要添加項,可以使用如下配置: ~~~ <archive> <manifestEntries> <Import-Package>javax.xml.ws.*</Import-Package> </manifestEntries> </archive> ~~~ **指定MANIFEST.MF文件** 還可以直接指定MANIFEST.MF文件。如下: ~~~ <archive> <manifestFile>META-INF/MANIFEST.MF</manifestFile> </archive> ~~~ # 例子一 在使用maven來管理項目時,項目除了web項目,還有可能為控制臺程序,一般用于開發一些后臺服務的程序。最近在工作中也遇到了這種場景,使用quartz開發一個任務調度程序。程序中依賴很多jar包,項目的啟動時只需要初始化spring容器即可 使用方法 使用一個簡單的基于spring框架的demo來做程序示例,來介紹maven assembly插件的使用方法。 項目中的代碼目錄如下: ![](https://box.kancloud.cn/ece6fd7b459bcb5eaca214b766780a29_181x318.png) 在以上代碼目錄中,assembly目錄下為打包的描述文件,下面會詳細介紹該文件,bin目錄下為啟動腳本以及readme等文件,main下為maven標準中建立的文件,java代碼以及配置文件位于該目錄下。 打包完成后壓縮包目錄如下: ![](https://box.kancloud.cn/25fb8851120f023c34b3ac0ebd63c4b7_502x117.png) 打包完成后,我們可以看到bin目錄來存放啟動腳本等文件,config為配置文件,lib下為運行時依賴的jar包。 使用maven assembly插件需要在pom文件中配置,添加這個插件 ~~~ <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <id>make-zip</id> <!-- 綁定到package生命周期階段上 --> <phase>package</phase> <goals> <!-- 綁定到package生命周期階段上 --> <goal>single</goal> </goals> <configuration> <descriptors> <!--描述文件路徑--> <descriptor>src/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> ~~~ 其中execution節點,我們配置了執行maven assembly插件的一些配置,descriptor節點配置指向assembly.xml的路徑。 在assembly.xml配置了,我們打包的目錄以及相應的設置 ~~~ <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>distribution</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> </fileSet> <fileSet> <directory>${project.basedir}\src\bin</directory> <outputDirectory>\bin</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> <!-- 將scope為runtime的依賴包打包到lib目錄下。 --> <scope>runtime</scope> </dependencySet> </dependencySets></assembly> ~~~ assembly.xml的配置項非常多,可以參考http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 以上只是用了很少的一部分。 format設置包輸出的格式,以上格式設置的為zip格式,目前還支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式 fileSet定義代碼目錄中與輸出目錄的映射,在該節點下還有 <includes/>,<excludes/>兩個非常有用的節點。 比如: ~~~ <fileSet> <directory>${project.basedir}\src\main\resources</directory> <outputDirectory>\</outputDirectory> <includes> <include>some/path</include> </includes> <excludes> <exclude>some/path1</exclude> </excludes> </fileSet> ~~~ 以上代碼表示歸檔時包括some/path,不包括some/path1 dependencySets節點下為依賴設置 在上述配置中,表示所有運行時依賴的jar包歸檔到lib目錄下。在上述截圖中lib目錄下的文件就是所有依賴的jar包 ![](https://box.kancloud.cn/0a839e7c4af5711894334d4d772bf9da_496x184.png) # 例子二 ## 需求 打一個zip包,包含如下: ![](https://box.kancloud.cn/075995f03976a2aa71afb0717a29ed25_532x141.png) bin為程序腳本,啟動和停止 lib為依賴包 根目錄下為配置文件和項目jar包 ## 插件了解 ![](https://box.kancloud.cn/66e293d4c9312fc24948a2d36bac1a40_769x227.png) ## 配置 ~~~ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <mainClass>com.jd.bt.ZuulApplication</mainClass> <!-- to create a class path to your dependecies you have to fill true in this field --> <!-- 增加classpath --> <addClasspath>true</addClasspath> <!-- classpath路徑前綴 --> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>./</Class-Path> </manifestEntries> </archive> <excludes> <!--注意從編譯結果目錄開始算目錄結構--> <exclude>/*.yml</exclude> <exclude>/*.properties</exclude> <exclude>/*.xml</exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- not append assembly id in release file name --> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/depolyment.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>dist</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ~~~ **對于maven-jar-plugin配置**   其中manifest的部分是核心,在可執行的jar文件中,打包后會在jar文件內的META-INF文件夾下,生成一個MANIFEST.MF文件,里面記錄了可執行文件的一些相關配置,比如像上面一段代碼中所配置的內容,這里面就配置了可執行jar文件未來讀取classpath的相對目錄位置在什么地方,以及引入的jar文件都有哪些,上面的配置就是classpath目錄是./   mainClass配置表示,哪個class作為程序的入口來執行   addClasspath配置表示,是否將依賴的classpath一起打包   classpathPrefix配置表示,依賴的classpath的前綴,也就是打包后生成的MANIFEST.MF文件里,引入的jar文件都會加上前綴,lib/,比如fastjson-1.2.7.jar,在mainfest文件里就會是lib/fastjson-1.2.7.jar   excludes配置表示,排除哪些文件夾不被打包進去   注意:其實maven-jar-plugin主要就是配置了MANIFEST.MF這個文件而已,就是讓可執行文件知道自己怎么執行,加載哪些文件執行的描述,剩下的工作交由maven-assembly-plugin來處理 最終壓縮的文件格式,為zip,fileSets中配置了需要將那些文件打包到最終壓縮包中,     如配置文件包括了啟動腳本bin文件夾,里面放著shell的啟動腳本,相關的配置文件src/main/resources,里面放著整個程序提取的properties等相關的配置文件      最終可運行的jar文件,使用了${project.build.directory}變量,也就是通過maven-jar-plugin生成的那個jar文件   dependencySets里面配置了依賴庫最終輸出到lib文件夾下,與上面的maven-jar-plugin配置生成的manifest文件路徑相對應,這樣可運行jar就會按照manifest的路徑來找相應的文件進行加載 **src/main/assembly/depolyment.xml配置** ~~~ <?xml version="1.0" encoding="UTF-8"?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <id>dist</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/bin</directory> <outputDirectory>bin/</outputDirectory> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>/</outputDirectory> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> <excludes> <exclude>${groupId}:${artifactId}</exclude> </excludes> </dependencySet> </dependencySets> </assembly> ~~~ **start.sh和stop.sh** `start.sh` ~~~ #!/bin/bash ARTIFACT_ID=jd-bt-microservice-gateway-zuul VERSION=0.0.1-SNAPSHOT main_jar=${ARTIFACT_ID}-${VERSION}.jar PIDS=`ps aux|grep ${main_jar} |grep -v "grep" |awk '{print $2}'` if [ ! -z "$PIDS" ]; then echo "${ARTIFACT_ID} already started!" echo "PID: $PIDS" exit 0; fi BIN_PATH="${JAVA_HOME}/bin" LOG_PATH="/Users/lihongxu/log/${ARTIFACT_ID}/" mkdir -p $LOG_PATH echo "ready start ${main_jar}"; nohup $BIN_PATH/java -server -Xms4096m -Xmx4096m -jar ../${main_jar} >$LOG_PATH/nohup.log 2>&1 & sleep 5 PIDS=`ps aux|grep ${main_jar} |grep -v "grep" |awk '{print $2}'` if [ ! -z "$PIDS" ]; then echo " ${ARTIFACT_ID} Started Successed,pid:${PIDS}" exit 0; else echo " ${ARTIFACT_ID} Started Failed" exit 1; fi ~~~ `stop.sh` ~~~ #!/bin/bash ARTIFACT_ID=jd-bt-microservice-gateway-zuul VERSION=0.0.1-SNAPSHOT main_jar=${ARTIFACT_ID}-${VERSION}.jar PIDS=`ps aux|grep ${main_jar} |grep -v "grep" |awk '{print $2}'` if [ ! -z "$PIDS" ]; then kill -9 "$PIDS" echo " ${ARTIFACT_ID} is stop !" echo " PID: $PIDS" exit 0; fi ~~~ ## 啟動 java -cp 和 -classpath 一樣,是指定類運行所依賴其他類的路徑,通常是類庫,jar包之類,需要全路徑到jar包,window上分號";",linux使用":" * 格式: `java -cp .;myClass.jar packname.mainclassname` 表達式支持通配符,例如: `java -cp .;c:\classes01\myClass.jar;c:\classes02\*.jar packname.mainclassname` * 運行jar `java -jar myClass.jar` 執行該命令時,會用到目錄META-INF\MANIFEST.MF文件,在該文件中,有一個叫Main-Class的參數,它說明了java -jar命令執行的類。 * 運行jar和cp 用maven導出的包中,如果沒有在pom文件中將依賴包打進去,是沒有依賴包。 1. 打包時指定了主類,可以直接用java -jar xxx.jar。 2. 打包是沒有指定主類,可以用java -cp xxx.jar 主類名稱(絕對路徑)。 3. 要引用其他的jar包,可以用java -classpath $CLASSPATH:xxxx.jar 主類名稱(絕對路徑)。其中 -classpath 指定需要引入的類。 實例 下面基于pom和META-INF\MANIFEST.MF兩個文件的配置,進行了三種情況的測試: pom.xml的build配置: ~~~ <build> <!--<finalName>test-1.0-SNAPSHOT</finalName>--> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>test.core.Core</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <!--下面是為了使用 mvn package命令,如果不加則使用mvn assembly--> <executions> <execution> <id>make-assemble</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ~~~ META-INF\MANIFEST.MF的內容: Manifest-Version: 1.0 Main-Class: test.core.Core 1. pom中build指定mainClass 但是 META-INF\MANIFEST.MF文件中沒有指定Main-Class: test.core.Core ~~~ java -jar test-jar-with-dependencies.jar //執行成功 java -cp test-jar-with-dependencies.jar test.core.Core //執行失敗,提示jar中沒有主清單屬性 ~~~ 2. pom中build沒有指定mainClass 但是 META-INF\MANIFEST.MF文件中指定了Main-Class: test.core.Core ~~~ java -jar test-jar-with-dependencies.jar //執行失敗,提示jar中沒有主清單屬性 java -cp test-jar-with-dependencies.jar test.core.Core //執行成功 ~~~ 3. pom中build指定mainClass && META-INF\MANIFEST.MF文件中增加了Main-Class: test.core.Core ~~~ java -cp test-jar-with-dependencies.jar test.core.Core //執行成功 java -jar test-jar-with-dependencies.jar //執行成功 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看