skip to Main Content

Simple question here but could not found it anywhere else.

Eclipse has this feature in Preferences > General > Workspace > Build where you can set the projects build order. Is there any way to do it on VS Code?

Using Maven.

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Altought @JialeDu is right and theres no emmbed way to do it, you can also try to make a task build in VS Code, here's how for future reference:

    1. Open your Java project in VS Code.
    2. Open the Command Palette (Ctrl/Cmd + Shift + P) and type "task" to see the available options.
    3. Choose the option "Tasks: Configure Task Runner".
    4. Select "Java" from the list of templates, if available, or choose "Others" if the Java template is not available.
    5. Enter the following task configuration:
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Build",
                "type": "shell",
                "command": "javac",
                "args": [
                    "${file}"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    
    1. Save the file as tasks.json in the .vscode directory of your project.

    Now you can build your Java project by opening the Command Palette and typing "run build task", or by using the keyboard shortcut (Ctrl/Cmd + Shift + B).

    If you have multiple Java projects in the same workspace, you can modify the args in the task configuration to specify which project to build. Here is an example:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Build Project 1",
                "type": "shell",
                "command": "javac",
                "args": [
                    "-cp",
                    "lib/*",
                    "src/main/java/com/project1/Main.java"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            },
            {
                "label": "Build Project 2",
                "type": "shell",
                "command": "javac",
                "args": [
                    "-cp",
                    "lib/*",
                    "src/main/java/com/project2/Main.java"
                ],
                "group": {
                    "kind": "build"
                }
            }
        ]
    }
    

    If your Java projects are also Maven projects, you can use the Maven command to build them instead of javac. Here is an example task configuration for building Maven projects:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Build Project 1",
                "type": "shell",
                "command": "mvn",
                "args": [
                    "clean",
                    "install"
                ],
                "options": {
                    "cwd": "${workspaceFolder}/project1"
                },
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            },
            {
                "label": "Build Project 2",
                "type": "shell",
                "command": "mvn",
                "args": [
                    "clean",
                    "install"
                ],
                "options": {
                    "cwd": "${workspaceFolder}/project2"
                },
                "group": {
                    "kind": "build"
                }
            }
        ]
    }
    

    In this example, the command is set to mvn and the args specify the clean and install goals. The options field is used to specify the current working directory (cwd) for each project, so that the correct Maven project is built. You can run either task by opening the Command Palette and typing "run build task", then selecting the desired task from the list.

    Taken from https://chat.openai.com/chat


  2. It seems that vscode currently does not have such an interface feature to set the build order of Java projects.

    eclipse
    enter image description here

    For more Java project management, you can check this link. If you have a need, you can file a report on GitHub to get a response from the official folks, maybe they will consider adding this.

    In addition, the same effect can be achieved by modifying pom.xml in maven. Pay attention to follow the rules of dependency resolution and version specifications, otherwise the build will fail.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search