eclipse maven .jar中沒有主清單屬性


報錯環境: windows系統eclipse maven 打包jar包后, 運行 java -jar 報錯

E:\My_java\mysql\target>java -jar original-mysql-0.0.1-SNAPSHOT.jar
original-mysql-0.0.1-SNAPSHOT.jar中沒有主清單屬性

解決方法

一.項目目錄

 

二. 首先在POM.XML 文件加入你用來打包的jar包的依賴,這里用shade

<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin -->
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
</dependency>

三.在POM文件加入build項

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                        <goal>shade</goal>
                </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.feilong.mysql.SelectTest</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
            </execution>
        </executions>
     </plugin>
  </plugins>
</build>

四. 完整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>

<!-- 反寫的公司網址+項目名 -->
  <groupId>org.feilong</groupId>
  
  <!-- 項目名 + 模塊名 -->
  <artifactId>mysql</artifactId>
  
  <!-- 版本號   SNAPSHOT 快照; alpha 內部測試; beta 公測; release 穩定 ga 正式版本-->
  <version>0.0.1-SNAPSHOT</version>
  
  <!-- 打包方式 例如 war zip pom-->
  <packaging>jar</packaging>

<!-- 項目描述名 -->
  <name>mysql</name>
  
  <!-- 項目地址 -->
  <url>http://maven.apache.org</url>
<!-- description  項目描述 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<!-- 依賴列表 -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      
      <!-- 依賴范圍, 表名 junit 只在test時有用-->
      <scope>test</scope>
     <!-- <optional>設置依賴是否可選</optional> -->
     <!-- exlusions   -->
    </dependency>
    
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.15</version>
</dependency>


    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.3</version>
    </dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.14</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin -->
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
</dependency>


  </dependencies>
 <!-- <dependencyManagement></dependencyManagement> 依賴管理-->
    
 <!--<build>
  插件 
 <plugins>
    <plugin>
    </plugin>
 </plugins>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${artifactId}-${version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
  </build>

-->
<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                        <goal>shade</goal>
                </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.feilong.mysql.SelectTest</mainClass> //這里不要寫成SelectTest.java 
                            </transformer>
                        </transformers>
                    </configuration>
            </execution>
        </executions>
     </plugin>
  </plugins>
</build>


</project>

 

成功運行

E:\My_java\mysql\target>java -jar mysql-0.0.1-SNAPSHOT.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
四月 01, 2019 9:13:32 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
create success
1, 24, 女
2, 50, 女
3, 76, 女

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM