Idea使用Maven打包jar

创建好JavaFX项目后

在pom.xml文件中,添加项目入口类


    
        
            org.apache.maven.plugins
            maven-shade-plugin
            1.2.1
            
                
                    package
                    
                        shade
                    
                    
                        
                            
                                com.lee.MainApp
                            
                        
                    
                
            
        
    

其中 com.lee.MainApp 是入口类包含所在包的完整名称

打开Maven Projects视图
Views -> Tool Windows -> Maven Projects

选择 package ,运行 Run Maven Build(绿色三角),或者直接双击pakcage

等待编译完成...

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building com.lee 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ com.lee ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ com.lee ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ com.lee ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\workspace\java_idea\JavaFXDemo2\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ com.lee ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ com.lee ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ com.lee ---
[INFO] Building jar: D:\workspace\java_idea\JavaFXDemo2\target\com.lee-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ com.lee ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing D:\workspace\java_idea\JavaFXDemo2\target\com.lee-1.0-SNAPSHOT.jar with D:\workspace\java_idea\JavaFXDemo2\target\com.lee-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.918 s
[INFO] Finished at: 2017-11-01T16:53:33+08:00
[INFO] Final Memory: 11M/232M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

在目录 D:\workspace\java_idea\JavaFXDemo2\target 中可以看到编译后的jar文件 com.lee-1.0-SNAPSHOT.jar

运行jar文件

java -jar com.lee-1.0-SNAPSHOT.jar

注意:
在 windows 中 com.lee-1.0-SNAPSHOT 可能不合法,重命名后可以运行

rename com.lee-1.0-SNAPSHOT JavaFXDemo.jar

参考:
Maven 项目生成jar运行时提示“没有主清单属性”

你可能感兴趣的:(Idea使用Maven打包jar)