Как сохранить проект в intellij idea java
Перейти к содержимому

Как сохранить проект в intellij idea java

  • автор:

Import and export projects

This option imports the selected project to IntelliJ IDEA as is (opens it). If you want to set custom settings while importing the project (for example, select another SDK or choose the libraries that you want to import), refer to Create a project from existing sources.

Launch IntelliJ IDEA.

If the Welcome screen opens, click Open .

Otherwise, from the main menu, select File | Open .

In the dialog that opens, select the directory in which your sources, libraries, and other assets are located and click Open .

When you import or clone a project for the first time, IntelliJ IDEA analyzes it. If the IDE detects more than one configuration (for example, Eclipse and Gradle), it prompts you to select which configuration you want to use.

If the project that you are importing uses a build tool, such as Maven or Gradle, we recommend that you select the build tool configuration.

Select the necessary configuration and click OK .

Dialog that prompts you to select how you want to import the project

The IDE pre-configures the project according to your choice. For example, if you select Gradle , IntelliJ IDEA executes its build scripts, loads dependencies, and so on.

If you have been working with another project, select whether you want to open the new project in a new dialog or in the current one.

For instructions on how to get a project from version control, refer to Check out a project from a remote host (clone).

Import a project with settings

This section describes the functionality that is available out of the box. If you are using a framework plugin, refer to the corresponding documentation section.

Import a project from an external model

Use this type of import if your project comes from an external model, and you want to import it as a whole. In this case, IntelliJ IDEA interprets the project files (for example, your Eclipse project will be migrated to IntelliJ IDEA).

Launch IntelliJ IDEA.

If the Welcome screen opens, press Control+Shift+A , type project from existing sources , and click the Import project from existing sources action in the popup.

Otherwise, from the main menu, select File | New | Project from Existing Sources .

In the dialog that opens, select the directory in which your sources, libraries, and other assets are located and click Open .

Select the external model that your project uses:

Gradle: select the necessary build tool and click Finish .

For Maven and Gradle projects, the IDE configures the settings automatically. You will be able to adjust them after the project is imported.

Create a project from existing sources

Use this type of import to create an IntelliJ IDEA project over the existing source code that is not necessarily an exported project.

Launch IntelliJ IDEA.

If the Welcome screen opens, press Control+Shift+A , type project from existing sources , and click the Import project from existing sources action in the popup.

Otherwise, from the main menu, select File | New | Project from Existing Sources .

In the dialog that opens, select the directory in which your sources, libraries, and other assets are located and click Open .

Select the Create project from existing sources option and click Next .

Specify the name and location and select a format for the new project. It’s recommended that you use the directory-based format.

If you are importing the project to the same directory, the IDE asks you whether you want to overwrite it. If you click Yes , IntelliJ IDEA will overwrite the files in .idea directory and the .iml files, your source files will remain intact.

Select the directories that you want to use as source root directories (folders with your source code) and click Next .

Select the libraries that you want to add to the new project.

You can join several selected libraries or archives into a new library by clicking or split the selected library into two by clicking .

Review module structure: select the modules that you want to include in your project.

You can merge several modules into one by clicking or split the selected module into two by clicking .

Specify the SDK that you want to use.

If the necessary SDK is already defined in IntelliJ IDEA, select it from the list on the left. Otherwise, click and add a new SDK.

Enable support for the detected frameworks and technologies: select checkboxes next to the necessary items.

You can also specify how the files-indicators should be grouped: by type (by framework) or by directory (by location).

Export a project

You can save a project as a .zip archive or export it to Eclipse.

Save a project as a .zip file

The option to export a project to a .zip file is available if the Android bundled plugin is enabled.

From the main menu, select File | Export | Project to Zip File .

In the dialog that opens, specify the path to which you want to save the .zip file with the project and click Save .

How to Export a JAR from IntelliJ

A JAR (Java archive) file is a platform-independent package of your application in a single archive file. Packaging your application into a JAR makes it easier to distribute your program, and it also means functionalities in your program are reusable—for example, other programs can use your functionalities just by adding your JAR file as a dependency.

There are two types of JARs: normal and executable. Normal JARs have no entry point, meaning you cannot directly execute this type of JAR. You can only add it as a dependency to other programs and access the classes and functions in your program. On the other hand, an executable JAR can be executed directly without any external program. Simply put, you can start an executable JAR directly just by double-clicking on it.

IntelliJ provides a couple different ways to export a JAR from your workspace. In this tutorial, I’ll explain the two different methods for setting up your project architecture to export a JAR from IntelliJ:

  • Building an artifact in IntelliJ
  • Using Maven

To understand the tutorial better, you’ll use a simple example program that accepts user profile information as a command line argument. This program also uses Picocli and Apache Commons Cli as external dependencies, which will support validating and parsing the command line parameters.

Export a JAR Using the Build Artifact Method

In this method, you’ll export a JAR using the IntelliJ build option. Before starting up, you’ll need to create a project and add the necessary dependencies to it. Dependencies are external programs packaged as a JAR with some functionalities implemented already and can easily be reused in your program. You’ll also need to create a main class with some simple functionality.

Once you’ve created a project and a Run Configuration, which can be used to execute the project, let’s set up your program to export a JAR from the IntelliJ workspace.

1. Configure the project settings to define the artifacts of this project. Click Project Structure from the file menu.

Setting up the project structure

2. In the Project Structure window, click Artifacts in the left pane, then click the plus symbol. Expand the JAR option and click From module with dependencies.

Creating artifacts

This opens the Create JAR from Modules window.

Configuring JARs

3. Click the folder icon on the right side of the Main Class field to select the main class of your project. If you know the name of the main class, start typing in the field to see suggestions.

4. Set how external libraries should be handled while creating the JAR. There are two options:

    • Extract to the target JAR: This option extracts all the class files of your external libraries to the JAR you’re creating. This single JAR will contain your class files and also the class files from your external dependencies. This option is recommended because handling a single JAR is easier than handling multiple. You can easily add it to your other programs and execute it with a double click.
    • Copy to the output directory and link via Manifest: This option copies your dependency JARs to the output directory and creates a link with your Java program using the manifest file. If you select this option, you should also specify a location where the manifest file needs to be created. This method is recommended if you have a lot of JARs, and particularly if any of them are signed. Signed JARs may not work properly when you extract the class files into the JAR. Therefore, it’s better to copy the JARs to the output directory and use them via the manifest file.

    5. Select the appropriate option and click OK, creating an artifact in your Artifacts window.

    Configuring artifacts

    You’ll see the name of the JAR in the Name field and its location in the Output directory. You can change the output directory to your desired location.

    If you need a JAR to be created during each and every project build, select Include in project build. If you don’t need that, leave the option unselected. In that case, you can build the JAR when you’re done with your project development, using the Build Artifacts option.

    Building artifacts

    When you click Build Artifacts, the JAR is generated in the output directory you’ve selected.

    So that’s how you export a JAR using the options available in IntelliJ. Next, you’ll see how to export a JAR using Maven.

    Exporting a JAR Using Maven

    Maven is a project management and automation tool, developed to make the build process easier. For example, you can create scripts to:

    • Build a project and create a JAR after each and every commit to your git repo.
    • Build a project nightly and deploy the JARs in the production systems when all the defined test cases are passing.
    • Automate build and deployment of projects with Jenkins.

    To begin, create a Maven project in IntelliJ using this tutorial. Remember to add the necessary classes for your project. You can use the same class files and dependencies used in the previous section’s example.

    When you create a new Maven project, you’ll have the minimal POM file created in your project directory with the following contents:

    This POM file contains three important artifacts for the project:

    • groupId : A unique base name of the company that creates and maintains the project.
    • artifactId : A unique name for the project itself
    • version : Version number for the project

    Configuring the Maven build

    In this section, you’ll configure the aspects of your Maven build. You can add various plugins inside the build section of your POM file based on your requirements. Each will have different functions. These plugins will be executed during the build of your project.

    In this tutorial, you’ll add the necessary plugin and configure it to export a JAR during the build.

    This adds a Maven compiler for your projects and also denotes the java version for compiling your sources. Here, you’re using Java version 1.8.

    2. Add the maven-assembly-plugin . This creates an assembly of your Java project.

    For a quick explanation of the plugin’s various sections:

    • artifactId : The name of the plugin itself
    • configuration : Configures the maven assembly
    • finalName : Final name of the assembly to be created : Flag to include or exclude the assembly name in the JAR’s final name : Adds instructions to the archive builder, in this case, adding the main class of your Java program in the manifest : List of references to assembly descriptors available on the plugin’s classpath
    • execution : Tag to specify the activities to be performed during execution
    • goal : Here, directory-single is used to create a single file during the build

    Now that you’ve configured the Maven settings for your project, the whole POM file for the project should look like this:

    Executing a Maven Build

    Next, you’ll create a configuration for the Maven build and execute it. It’ll export a JAR in the directories you’ve specified.

    1. Navigate to Run > Edit Configurations. The Run/Debug Configurations window opens.

    Editing configurations

    2. Select the Maven option from the Add New Configuration menu.

    Adding new configuration

    This will create a Maven run configuration for you as below.

    Configuring new Maven configuration

    3. Give the configuration a name and add the project’s home directory as your home directory.

    4. In the command line field, add clean install . These goals stipulate that the projects should be cleaned and the package should be installed in the appropriate directory.

    5. Click Apply and OK. This creates a run configuration for you directly.

    6. From the Run menu, click Run configuration_name.

    Run configuration

    You’ll see the build starting in your console, and after a few seconds, you’ll see the message BUILD SUCCESS . The JAR will be in the target directory of your Project home.

    Remember, since this project has the configurations in a Pom.xml file, you can also configure an automatic build using Jenkins scripts on any trigger (for example, after every commit to your git repo).

    Conclusion

    Building a JAR in IntelliJ is a little complicated when it comes to configuring project structure and generating artifacts. In the case of using Maven, it’s even more difficult due to the setup involved with configuring the POM file and the project’s build processes.

    But in this tutorial, you learned how to tackle two different options for building a JAR (using the build artifact method and using Maven) in IntelliJ to make the process easier. If you’ve got any suggestions or tips for creating a JAR that this article didn’t cover, please let me know about them in the comments.

    How to export and import java project with intelij

    I am having an issue that I am trying to solve. I have a java project and I use intelij IDE. I want to move it to another computer. I used to work with eclipse and this task was very easy, export as zip and import and open zip. However in intelij there is no way to do it, it is not support zip. I searched all the web, and nothing, all the solutions are with git. all the information on youtube and so are just to import project to intelij, or to export as jar and run the project. what is the easiest way without using the internet to export project from one intelij using disk on key, and open it and start working on another computer with intelij. Can anyone provide step by step solution for this question. I am using 2019 IDE community version regards

    Bastian's user avatar

    2 Answers 2

    Well the answer is «it depends.» For many maven projects, the IDE files are not checked in. When you get a new machine, clone the repo and import the project. Yes, you start from scratch, but it is easy. From the IDE, you may have to manually select Java, and setup Maven. Again this depends. Some projects use bundled Java and Maven, and other groups manually install specific versions. So as I said, it depends upon the project.

    With Intellij, projects can generally be copied from one directory to another. This means that the paths in the IntelliJ iml files use relative paths.

    So this is really a build question. Personally, I want to be able to build from source control. I will check in Intellij runtime configurations, but have git ignore other IDE files.

    Perhaps you could clarify what issues you have building from freshly cloned repo.

    Как выгрузить созданную программу из Intellij Idea?

    Нажмите ПКМ внутри main метода и затем run. Если точнее «run <имя класса*>.main()»
    * Имя класса, в котором находится метод main.

    Но если вы хотите не просто запустить, а собрать Runnable Jar файл из всего проекта, то так:
    File -> Project Strucure -> Artifacts -> «+» (слева наверху, чуть правее угла) -> JAR -> From modules with dependencies. -> Выбираете Class с main методом -> Ok.

    Всё, вы всё настроили, теперь, чтобы создать jar файл, надо нажать:
    Build -> Build Artifacts -> выбрать Build в открывшемся списке.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *