2017-10-21 82 views
0

虽然从IDE项目executes,是如何创建的,这样一个Hello World类型的控制台应用程序将实际运行JARMVN清洁套装:无主清单属性

[email protected]:~/NetBeansProjects/HelloMaven$ 
[email protected]:~/NetBeansProjects/HelloMaven$ mvn clean package 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building HelloMaven 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloMaven --- 
[INFO] Deleting /home/thufir/NetBeansProjects/HelloMaven/target 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloMaven --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloMaven --- 
[INFO] Changes detected - recompiling the module! 
[INFO] Compiling 1 source file to /home/thufir/NetBeansProjects/HelloMaven/target/classes 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloMaven --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory /home/thufir/NetBeansProjects/HelloMaven/src/test/resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloMaven --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloMaven --- 
[INFO] No tests to run. 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloMaven --- 
[INFO] Building jar: /home/thufir/NetBeansProjects/HelloMaven/target/HelloMaven-1.0-SNAPSHOT.jar 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 3.400 s 
[INFO] Finished at: 2017-10-21T10:50:42-07:00 
[INFO] Final Memory: 14M/47M 
[INFO] ------------------------------------------------------------------------ 
[email protected]:~/NetBeansProjects/HelloMaven$ 
[email protected]:~/NetBeansProjects/HelloMaven$ tree 
. 
├── nbactions.xml 
├── pom.xml 
├── src 
│   ├── main 
│   │   ├── java 
│   │   │   └── net 
│   │   │    └── bounceme 
│   │   │     └── dur 
│   │   │      └── hello_maven 
│   │   │       └── Main.java 
│   │   └── resources 
│   └── test 
│    └── java 
└── target 
    ├── classes 
    │   └── net 
    │    └── bounceme 
    │     └── dur 
    │      └── hello_maven 
    │       └── Main.class 
    ├── generated-sources 
    │   └── annotations 
    ├── HelloMaven-1.0-SNAPSHOT.jar 
    ├── maven-archiver 
    │   └── pom.properties 
    └── maven-status 
     └── maven-compiler-plugin 
      ├── compile 
      │   └── default-compile 
      │    ├── createdFiles.lst 
      │    └── inputFiles.lst 
      └── testCompile 
       └── default-testCompile 
        └── inputFiles.lst 

25 directories, 9 files 
[email protected]:~/NetBeansProjects/HelloMaven$ 
[email protected]:~/NetBeansProjects/HelloMaven$ java -jar target/HelloMaven-1.0-SNAPSHOT.jar 
no main manifest attribute, in target/HelloMaven-1.0-SNAPSHOT.jar 
[email protected]:~/NetBeansProjects/HelloMaven$ 

显然只是需要指定清单的Main-Class条目。

只需专注于packagephase

运行Maven工具Maven的阶段

虽然几乎没有一个全面的列表,这些都是执行最常见的 默认的生命周期阶段。

验证:验证项目是正确的,所有必要的 信息可编译:编译 项目测试的源代码:测试使用合适的单元 测试框架已编译的源代码。这些测试应该不需要代码被打包 或部署的数据包:把编译后的代码,并在其 分发格式打包,例如一个JAR。集成测试:进程和 将程序包部署到可以运行集成 的环境中验证:运行任何检查以验证程序包有效 并且符合质量标准安装:将程序包安装到本地 存储库中用作其他项目的依赖本地部署: 在整合或者发布环境,复制最终 包到远程仓库与其他开发者共享和 项目

+1

的[无法执行JAR-文件: “无主清单属性”]可能的复制(https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no -main-清单属性)。或者具体到maven https://stackoverflow.com/questions/15990258/maven-cant-execute-jar – nullpointer

回答

0

我明白了,这是一个使用的问题阴影插件:

[email protected]:~/NetBeansProjects/HelloMaven$ 
[email protected]:~/NetBeansProjects/HelloMaven$ mvn clean package 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building HelloMaven 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloMaven --- 
[INFO] Deleting /home/thufir/NetBeansProjects/HelloMaven/target 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloMaven --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloMaven --- 
[INFO] Changes detected - recompiling the module! 
[INFO] Compiling 1 source file to /home/thufir/NetBeansProjects/HelloMaven/target/classes 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloMaven --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory /home/thufir/NetBeansProjects/HelloMaven/src/test/resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloMaven --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloMaven --- 
[INFO] No tests to run. 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloMaven --- 
[INFO] Building jar: /home/thufir/NetBeansProjects/HelloMaven/target/HelloMaven-1.0-SNAPSHOT.jar 
[INFO] 
[INFO] --- maven-shade-plugin:3.1.0:shade (default) @ HelloMaven --- 
[INFO] Replacing original artifact with shaded artifact. 
[INFO] Replacing /home/thufir/NetBeansProjects/HelloMaven/target/HelloMaven-1.0-SNAPSHOT.jar with /home/thufir/NetBeansProjects/HelloMaven/target/HelloMaven-1.0-SNAPSHOT-shaded.jar 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 3.805 s 
[INFO] Finished at: 2017-10-21T11:02:31-07:00 
[INFO] Final Memory: 17M/56M 
[INFO] ------------------------------------------------------------------------ 
[email protected]:~/NetBeansProjects/HelloMaven$ 
[email protected]:~/NetBeansProjects/HelloMaven$ java -jar target/HelloMaven-1.0-SNAPSHOT.jar 
Oct 21, 2017 11:02:38 AM net.bounceme.dur.hello_maven.Main getGreeting 
INFO: Hello world. 
[email protected]:~/NetBeansProjects/HelloMaven$ 
[email protected]:~/NetBeansProjects/HelloMaven$ cat 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.mycompany</groupId> 
    <artifactId>HelloMaven</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-shade-plugin</artifactId> 
       <version>3.1.0</version> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>shade</goal> 
         </goals> 
         <configuration> 
          <transformers> 
           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
            <mainClass>net.bounceme.dur.hello_maven.Main</mainClass> 
           </transformer> 
          </transformers> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 
[email protected]:~/NetBeansProjects/HelloMaven$ 

http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html