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

 

posted @ 2019-04-01 21:15  江期玉  阅读(5112)  评论(0编辑  收藏  举报