java控制台程序打包为jar


1. 创建maven空项目,不需要选择Create from archetype.

创建一个Main方法作为程序的入口.

2. 修改pom.xml


    
        org.apache.maven.plugins
        maven-compiler-plugin
        3.8.1
        
            8
            8
        
    
    
        org.apache.maven.plugins
        maven-shade-plugin
        3.2.0
        
            
                package
                
                    shade
                
                
                    
                        
                            
                            com.example.demo.Main
                        
                    
                
            
        
    

3. 点击Maven工具栏,再点击package,进行打包.

4. 如果有资源文件

在类中使用

File file = new File(Main.class.getClassLoader().getResource("test.txt").getFile());
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("test.txt");

在pom.xml里面配置资源文件


    
        src/main/resources
        
            *.*
        
        false
    

5. 可以配置jar包名

test

6. 如果有编码文件


    UTF-8

最后的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

    4.0.0

    org.example
    demo
    1.0-SNAPSHOT
    
        UTF-8
    
    

    

    
        test
        
            
                src/main/resources
                
                    *.*
                
                false
            
        

        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    8
                    8
                
            
            
                org.apache.maven.plugins
                maven-shade-plugin
                3.2.0
                
                    
                        package
                        
                            shade
                        
                        
                            
                                
                                    
                                    com.example.demo.Main