<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 遇到的問題: 此處耗時2天時間,遇到過的坑: 1.修改完pom.xml后,不生效。 --改pom.xml后,代碼不生效,是因為對IDEA工具不熟,在修改完xml后,需要點工具右下角的import changes或者直接點auto-import就可以一勞永逸了。 2.生成jar后,idea可以執行,但是java -jar無法執行,報錯Exception in thread "main" java.lang.NoClassDefFoundError 如果修改pom.xml中的mainClass生效了,說不定是mainClass傳入的不對,使用mvn exec:java -Dexec.mainClass="com.delon.main.Test"可以嘗試main方法是否正確。 如果想用編譯Test.java文件,可以使用mvn clean compile exec:java -Dexec.mainClass="com.delon.main.Test" 3.生成jar后,idea可以執行,java -jar也可以執行,但是缺少相關依賴,報錯Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/RequestBody 參考如下解決方式即可。 # maven構建jar包的步驟: 1.執行可執行的class,代碼內需要有入口main方法 2.通過mvn package來構建jar包 3.使用java -jar test.jar來執行jar包 # 包含依賴jar包 maven的pom.xml配置文件 ~~~ <?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.xxx.delon</groupId> <artifactId>bugly</artifactId> <version>1.0-SNAPSHOT</version> <dependencies>     <!-- 工程所需jar包引用開始 --> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <!-- 代碼所需依賴 --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.3.0</version> </dependency> <!-- 代碼所需依賴 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.2</version> </dependency> <!-- 工程所需jar包引用開始 --> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <!-- 此處指定main方法入口的class --> <mainClass>com.xxx.uploadFile</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> ~~~ 生成jar包,會生成在target目錄下 ~~~ mvn package ~~~ 解壓縮bugly-1.0-SNAPSHOT.jar->META-INF->MANIFEST.MF ~~~ Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: delon Build-Jdk: 1.8.0_112 Main-Class: com.xxx.uploadFile ~~~ 執行: ~~~ # 運行jar java -jar test.jar ~~~ 也可以通過如下命令 ~~~ mvn assembly:assembly #跳過測試 mvn -Dmaven.test.skip=true assembly:assembly ~~~ 注意:在執行這個命令之前,必須先配置Maven的環境變量,檢查是否配置可通過命令: mvn -version 如果上面的命令成功執行,那么在項目路徑的target文件下就會有兩個jar文件,一個是有jar包依賴的,一個是沒jar包依賴 # 不包含依賴jar包 如果不想包含依賴的jar包,可以把<build>里面的代碼替換成如下code ~~~ <!-- 打包jar文件時,配置manifest文件,加入lib包的jar依賴 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.xxx.uploadFile</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 拷貝依賴的jar包到lib目錄 --> <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}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> ~~~ # 只包含部分依賴jar包 如果想只包含部分依賴jar包 比如說,想做一個工具jar包,依賴公共jar和自己本地jar包,本地jar包需要解壓成class打到jar包內,而依賴的公共jar包則不需要。 剔除公共jar包 可以用`<scope>` `<scope>`的值的含義: compile,缺省值,適用于所有階段,會隨著項目一起發布。 provided,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。 runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。 test,只在測試時使用,用于編譯和運行測試代碼。不會隨項目發布。 system,類似provided,需要顯式提供包含依賴的jar,Maven不會在Repository中查找它。 編譯的時候采用 compile ~~~ <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <scope>complie</scope> <optional>true</optional> </dependency> ~~~ 在用package打包的時候,改成test,生成的jar包里就不會有該jar包的類了。 ~~~ <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <scope>test</scope> <optional>true</optional> </dependency> ~~~ build配置項,mainClass為空因為不是可執行jar。 ~~~ <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass></mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ~~~
                  <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>

                              哎呀哎呀视频在线观看