Skip to content

Export Your Project to a Jar

CsCherrYY edited this page Nov 20, 2020 · 9 revisions

Currently, the Java Project Manager offers two effective modes to help you export the project to a jar. The primary one is an exporting wizard, which will guide you to export your project to a jar by some steps. The advanced one is exporting jar by custom tasks. You can define your own task by assigning 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

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

image

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

image

If you use inline button to export jar, the export process will automatically export the chosen project. However, if your workspace has only one workspace folder, no action is required in this step.

Determine Main Class

Then, if one or more executable classes containing main method are found in your project, 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 or click the back button on the top left corner of the wizard to return to the last 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 project:

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. This will start the export process and show progress message in the status bar.

Success

If everything goes well, you will see a notification to tell 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 export jar. VS Code variables are allowed in this setting. If you want to select the output location manually when exporting the jar file, simply leave it empty or set it to askUser.

image

Advanced: Custom Export Task

For advanced usages such as automatic exporting jar, you can use export task to customize what to export. The available template of tasks can be found at Terminal->Configure Tasks.... Here is an example of tasks.json including an export jar task:

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

The following attributes are mandatory for every task configuration:

  • type - the type of export jar task, should always be java.
  • 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.

Here are some optional attributes available to all launch configurations:

  • mainClass - the mainClass in MANIFEST.MF. If this attribute is not specified, the wizard to select the mainClass will appear during the export process.
  • targetPath - the target path of jar. VS Code variables is 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 project 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 includes more than one workspace folder, one of them is demo. The ${compileOutput} will stand for the folders containing output class files in the runtime scope of all the workspace folders while the ${compileOutput:demo} stands for those of folder demo only.

You can run the custom task by clicking Terminal->Run Task and select the java type. Then the wizard will show all the available export jar tasks.

Clone this wiki locally