Skip to content

Export Your Project to a Jar

Shi Chen edited this page Jul 27, 2022 · 9 revisions

Currently, the Java Project Manager offers two effective modes to help you export a jar file from a workspace folder. The primary one offers an exporting wizard, which will guide you to export your workspace to a jar by some steps. The advanced one exports jar by custom tasks. You can create your own export task by defining some attributes. You can run it to export jar without any extra step then.

Export Wizard

In the Java Project explorer, you can see an export button on the title. When you hover on it, you will see Export Jar... as the following picture shows:

image

You can click this button to start the export process.

Determine Workspace folder

If your VS Code workspace has multiple workspace folders, you may choose one workspace folder to export:

image

The other way to export jar with a specific workspace is clicking the inline button in the workspace of the Java Project explorer, as the following picture shows:

image

note: If you use inline button to export jar, the export process will automatically export the chosen workspace folder.

Determine Main Class

Then, if one or more executable classes containing main method are found in your workspace, you will see the wizard to choose the main class:

image

In this step, your chosen main class will be the Main-Class attribute in the MANIFEST.MF of the output jar. You can choose <without main class> to keep the Main-Class of the MANIFEST.MF empty.

note: You can click the back button on the top left corner of the wizard to return to the last step. If no executable class is found in the workspace folder, no action is required in this step.

Determine Elements

After that, you will see the wizard choosing the elements of the output jar. You will see a list including output folders and the dependency artifacts of the workspace:

image

As shown in the above picture, each element has a description to present the scope. The runtime elements are chosen by default while the test ones are not. You can choose the elements you need and click OK.

note: If only one element is found in your workspace, this step will be skipped and the element will be included in the export jar automatically.

After that, the export process will start and show progress message in the status bar.

Success

If everything goes well, you will see a notification telling you that the export process is successfully finished.

image

Target Path Setting

You can change the setting java.project.exportJar.targetPath to specific the output path of the exported jar. You can leave it empty if you want to manually select the output location.

image

note: VS Code variables are allowed in this setting. For example, you can use ${workspaceFolder} to represent the path of workspace folder. To know more about VS Code variables, see: VS Code Variables Reference.

Advanced: Custom Export Task

For advanced usages such as automatic exporting jar, you can customize what to export by changing the attributes of export task. The available template of tasks can be found at Terminal->Configure Tasks....

image

Here is an example of tasks.json including an export jar task:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "java (buildArtifact)",
      "label": "java (buildArtifact):demo",
      "mainClass": "demo.App",
      "targetPath": "${workspaceFolder}/${workspaceFolderBasename}.jar",
      "elements": [
        "${compileOutput}",
        "${testCompileOutput}",
        "${dependencies}",
        "${testDependencies}",
        "!${compileOutput}/demo/innerPkg",
        "C:/work/demo/LICENSE"
      ],
      "problemMatcher": []
    }
  ]
}

The following attributes are mandatory for every task configuration:

  • type - the type of export jar task, should always be java (buildArtifact).
  • label - the name of export jar task. When you are going to run a task from Terminal -> Run Task..., the labels of all available tasks will appear in the list.

Note: To distinguish the custom task and the default task, we recommend changing the default label of the task.

Here are some optional attributes available to all launch configurations:

  • mainClass - the Main-Class in MANIFEST.MF. If this attribute is not specified, the wizard to select the Main class will appear during the export process.
  • targetPath - the target path of jar. VS Code variables are available in this attribute. If this attribute is not specified, the export process will apply the setting java.project.exportJar.targetPath.
  • elements - The array of elements to export. If this attribute is not specified, the wizard to select the elements will appear during the export process. This attribute is using glob patterns so you can also use ! to specify which not to include. Both absolute path and relative path to the workspace folder are supported here.

These reserved words are available in the elements:

  • ${compileOutput} - the folders containing output class files in the runtime scope.
  • ${testCompileOutput} - the folders containing output class files in the test scope.
  • ${dependencies} - the artifact dependencies in the runtime scope.
  • ${testDependencies} - the artifact dependencies in the test scope.

note : The reserved words can be used to stand for a specific project. For example, if your workspace folder includes more than one project, one of them is demo. The ${compileOutput} will stand for the folders containing output class files in the runtime scope of all the projects while the ${compileOutput:demo} stands for those of the project demo only.

You can run the custom task by clicking Terminal -> Run Task :

image

and select java (buildArtifact):

image

Then the wizard will show all the available export jar tasks. You can find your custom task here and click it to export your customized jar.