How to build jars from IntelliJ properly?

前端 未结 20 2826
盖世英雄少女心
盖世英雄少女心 2020-11-22 01:14

I have a project that contains a single module, and some dependencies. I\'d like to create a jar, in a separate directory, that contains the compiled module. In addition, I\

20条回答
  •  我寻月下人不归
    2020-11-22 01:59

    Here are 2 examples with maven project, step by step:

    Method 1: Build jar with maven and pom.xml

    1. File, new, project, maven
    2. create a package , create a java program inside that package
    3. at pom.xml, add maven-jar-plugin.

          
              
              org.apache.maven.plugins
              maven-jar-plugin
              3.1.0
              
                  
                      
                          true
                          lib/
                          somePackage.sample
                      
                  
              
          
      

    screen capture: 4. open maven project box by click on the search icon and type maven,

    1. click on clean, then click on install

    2. your jar file will show up inside the target folder

    3. test your jar using java -jar

    Method 2: Build jar with maven without pom.xml change

    1. File, new, project, maven
    2. create a package , create a java program inside that package (same as above)
    3. File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies

    Very important!

    The MANIFEST.MF needs to be in your resources folder and the Main.Class needs to refer to {package}-{class-name-that-contains-main-class}

    1. then click build on menu , build artifacts, build

    1. find the jar in the out folder

    1. run it :)

提交回复
热议问题