A little experience of transferring from Eclipse to IntelliJ IDEA (transfer)

Disclaimer: This question is transferred from other places, so it is convenient to find it later.

 

I haven't used IntelliJ IDEA for a long time. After using it for a while, I think it is really easy to use. Many people who have just transferred from Eclipse may not be comfortable at first, so I will share some experience and common functions in the process of use. Of course, before reading this article, I recommend you to read the 20 features of automatic code completion of IntelliJ IDEA. This article introduces the features of auto-completion very well, but the description of shortcut keys can be more perfect, which can take care of children's shoes with inconsistent default binding keys. This article describes the shortcut keys based on the default keymap of Mac OSX 10.5+. Of course, related text descriptions will also be provided. If it is inconsistent with my binding, you can search directly in the keymap.

basic settings

show line number

This used to be a simple question in Eclipse, and has been asked by some people since moving to IntelliJ. After setting it in the same way, it is found that it only works on the current file, and there is still no line number when opening another file. It's as if you were just executing the command in vim set number, instead of setting the same effect in .vimrc. This needs to be set in IDE Settings, just check it as shown in the figure:  imageOther settings can be checked by referring to this setting item, such as displaying spaces and so on.

File and Navigation Associations

When we often need to open a file when writing code, we want to automatically open the associated location on the file tree on the left, so that it is very convenient to find other files or other related files in this directory. In fact, this IntelliJ provides There are several solutions, the first one is the same way as Eclipse, as follows:

  • It used to be very simple to set up in Eclipse, but IntelliJ is a little different. If you want to temporarily, click the icon of the small globe (it looks like a small globe at first glance) when you open the file  image. If you want to be the same as the one in Eclipse, if you want to make all this automatic, then you can see two options AutoScroll to Sourceand in the floating layer of the above picture AutoScroll from source. IntelliJ is very user-friendly and divided into two settings. You check both of them. Basically, the functions of Eclipse and Eclipse are completely consistent, and it is natural to just check one.

Another way is to use the Navigation Bar provided by IntelliJ. Many functional operations of IntelliJ can be keyboardized. This is a necessary function for those who like keyboard operation. The operation method is to use the shortcut key when opening the file ⌘+UP (Jump to Navigation Bar), and then it will appear A drop-down is basically the file list of the directory where the file is located, and you can also use the up, down, left, and right keys to select other directories, isn't it cool?

Note: It is recommended to View > Navigation Barclick the option here, so that after using the shortcut key, it will appear at the position of your mouse, and it will feel more input. Of course, you will basically lose the right to use the mouse. This requires you to use it according to your personal preferences Just use it.

Use shortcut keys

If you are an Apple computer user, it is recommended that you change to the key binding of Mac OSX 10.5+, because the key binding of the Windows-compatible version of Mac OSX 10.5 is really lame, and it is really uncomfortable to use, just like your default habit ⌘+wis to close the label, and in this key binding is actually a shortcut for selecting text according to the symbol.

Here we mainly introduce a few shortcut keys that may be commonly used by everyone, or you may not know a few shortcut keys in IntelliJ. For the most commonly used ones, you can directly refer to the Keymap on the official website to introduce Getting Started . Learning to query the shortcut keys you need in Keymap is the main thing. Don't open the useless Baidu every time. For details, please refer to the link given at the end of this article.

Outline call out

One of the shortcut keys used in Eclipse in the past is ctrl+othat it can call up all the member variables and methods of the code file, and it can support search. It is very convenient. The basic function of IntelliJ is naturally there. The shortcut key for this in IntellJ is ⌘+F12 (File Structure). If you are not Mac OSX 10.5+ key bindings, the content in parentheses is what you can search in Keymap.

Basic operation

  • Copy the whole line or block: used in Eclipse ctrl+alt+Up, IntelliJ default is ⌘+D (Duplicate Line or Block), in fact, there is a gap between this and Eclipse, especially when there are multiple lines or when the content is selected, you can try it if you don't believe it. So you can Duplicate Linesdefine a shortcut key in the Keymap. What I defined is Option+Dthat this key does not have any shortcut key in my default binding. You can bind one and try it yourself.
  • Select text according to symbol: This key is used too much, and it is also very easy to use. For example, there is such a line of code that <name>SpringSource Milestones Proxy</name> your cursor is at a certain position in Milestones, and you want to replace SpringSource Milestones Proxyit with other words. Suppose you want to replace it with Test XX, in fact, you only need Option+Up (Select Word at Caret)two clicks to select the word you want. When you want to use it in the development process, you will find that this key is used too much, so much that you can change other editors that do not have this key. Or IDE, it is not used to it.
  • Delete the entire line:  You can directly ⌘+delete (Delete Line)or use IntelliJ's intelligent function, the shortcut key of cut, when the text is selected in this line, the cut `⌘+xwill be cut according to the selected text, but when you do not have any selected text in the line, it will be automatically cut Cut the whole line, then it is naturally similar to a shortcut key to delete the line, but this is actually quite lame, it is recommended to use it ⌘+delete.
  • Want to jump directly to the implementation method of the interface: ⌘+option+鼠标单击 or ⌘+option+B.
  • Statement completion Statement completion: This has been explained in IntelliJ IDEA's 20 code auto-completion features . I will focus on it here, because it is really easy to use, and it is strongly recommended. As mentioned in the text 智能自动完成 ctrl+shift+Space, when it gives you a hint Too many times, this key will definitely surprise you.
  • The same jump between words: The shortcut key in Eclipse is ctrl+k, this is very useful, and it is not lacking in IntelliJ, but it is empty by default in the key bindings of Mac OSX 10.5+. It is said that there is a binding in the default binding of Windows. You can check it yourself. I have bound one myself ⌘+ctrl+k (Find Word at Caret), and if there is none, I need to bind one myself, and then I can combine other keys to achieve the previous and ⌘+G (Find Next)next ⌘+Shift+G (Find Previous)functions. Very is practical.

Version control settings

We must have version control in our development. In the past, when you used Eclipse, when the local files and the repository were inconsistent, then the file and the folder where it was located would have a representation. You can easily see which local files have been modified. But this is not the case in IntelliJ by default. It only provides color changes for the changed files when they are modified, excluding each parent folder where they are located. If you want to be the same as Eclipse, you need to check the settings as shown below.
vcs_show_dir_statusIn fact, just look at the English explanation. It can be understood that after this default is enabled, if you want to adjust the display color of the folder, you can directly Settings > Editor > Colors & Fonts > File Statusadjust it, and you can adjust it to your liking.

Java resource files are displayed in non-English

IntelliJ personally thinks that IntelliJ is better than Eclipse in terms of resource files. It is quite convenient to modify a property in the Resource Bundle mode. You can modify the properties of all languages ​​on one operation interface. All this is done automatically, as shown in the figure below. Show:resource_bundle

And it is very intimate to have a sorting and grouping function, especially this sorting.

There is also a problem with the default settings, that is, Chinese will be displayed as Unicode by default. In fact, Settings > File Encodingsyou can check a setting ( ), as shown below:
resource_native_to_ascii

After this setting, all Unicode displays can be automatically converted to the language they should be displayed in.

How do web projects that conform to the Maven convention structure work better?

I remember that when I first started using Eclipse, Java EE projects were not supported by default, and a plug-in for MyEclipse had to be used. It seems that some people are still using this thing. At that time, after each Web project was modified, it must be Deployed and modified. things will be executed, which is very painful. Later, I used the Tomcat plug-in myself, which ensures that when you modify jsp and static files, they can be executed directly without Deploy, because the directory pointing to Tomcat actually points directly to your project file, which really saves a lot of worry.

In IntelliJ, if you have a traditional Java EE structure (there are structures such as src and webMoudle under the root directory), then you can add a Tomcat directly in Run Configurations, and click the + sign to add it (of course, you need to pay attention to it, although no Search box, you enter characters and it will automatically filter the results for you), so if you modify the Java file when running, you need to actively Make Project ⌘+F9 (Make Project). But if you have a project structure that conforms to the maven convention, it is not ideal if you continue to configure it like this. Every time you modify the jsp file, you need Make Project, because IntelliJ will automatically give you DeployMent after each Make Project, which seems to return to the troublesome environment a few years ago.

This problem is actually very easy to solve, that is, don't use this method to run your web project. Since the project that conforms to the maven structure naturally uses maven or gradle as a build tool, then you need to use maven's jetty plugin to run the project That's it, you can configure it as shown below: idea-maven-run

Of course, you can also click on the run as shown below to automatically add one:

maven-jetty-plugin-run
Referring to the picture above, it is not necessary to use jetty, it can also be run with the plug-in of tomcat6 or tomcat7.

How to import and run Eclipse's web structure project

In view of some users who have not yet converted to Maven to build projects, of course, I also sincerely say that building this piece should be turned quickly. Because if it is not turned, this chapter will be published separately, and some operations are still quite troublesome, the following also It will be mentioned, let's come together one by one, there are two choices when importing, and then I will explain in two parts, click File > Import Project…, and then select your project directory, click OK, as shown below:

image

Import using new project

In the first method shown in the figure, the import is basically all the way to next to completion, but for the first time, you can also pay attention to what you do in each step, for example, you can analyze the jars for you, if you use Spring also analyzes all your configuration files for you. After the final completion, the project will start to build the index. After everything is ready, we can complete the basic configuration. Since the web project can basically run on servers such as Tomcat, it is basically successful. Main points:

  • Take care of dependencies, because we don't use maven, so you'll have to do it yourself
  • Set the compile path for java files, if needed
  • Add your Tomcat and run successfully

dependencies

Open File > Project Structure , in this section we mainly use the following parts marked in red:  project_structure
Let's first understand these main components:

  • SDKs: Mainly store our JDK, etc., as shown below (I downloaded the JDK with source myself, so that I can directly view the Java source code related to the JDK): project_structure_sdks
  • Global Libraries: Mainly public jar files. For example, several jars of servlet may be required for each project, but the variables of Eclipse become difficult to use here, so you need to use this place to configure, as shown in the following figure: project_strutcture_global
  • Facets: I will not introduce it here, if necessary, it will be added later
  • Libraries: This is basically the library of the project, not the global one. The imported project will generally be in your Eclipse. All dependencies on the classpath are imported as a lib library and then applied to your project's modules
  • Modules: This basically includes the modules of the project. Generally speaking, the project structure of Eclipse is a single module.

The configuration we need to do is mainly in Modules, and the others can be configured according to our understanding.

  1. Let's look at the Sources in Modules first , as shown in the figure below:  Just add all the source code of the project. As shown in the figure, my project is actually missing the resource folder, so I can select the resource and click Sources, this will Add a resource folder to the Source Folders on the left.project_structure_modules_sources
  2. Then there is the Paths part, which is mainly the output path of the class. The default is in the out folder in the root directory. If there is a dead path for the parsing of configuration files in the project, it must strictly follow the specifications of Java EE. Modify this part as follows (ignore this step if there is no situation I assume):
    project_structure_modules_paths
  3. Then Dependencies, it is better to configure, just add the required dependencies.

After these steps are completed, we can click Run > Edit Configurations , click the + sign in the upper left corner, and then select **Tomcat > Local Server**, these steps are similar to Eclipse, select the corresponding path, etc., just need to pay attention to one thing, that is, you need to add a DeployMent, as shown below:
run_configurations_tomcat

If it is the + sign in DeployMent in the Windows picture, it may be on the right side . After clicking, a dialog box will pop up to select the folder. Select the default webMoudle folder of eclipse (MyEclipse is the WebRoot folder). This must not be wrong. , do not select it as the main folder of the project, otherwise the operation will not succeed, and then ctrl+Rrun it. Just run it in debug mode ctrl+D.

Import a project using IntelliJ's support for Eclipse projects

The basic reference to the previous one is basically the same, that is, the steps are less than the previous one. If there is a problem, you can refer to the previous chapter.

HQL query support

In the past, Eclipse had an HQL plug-in that could directly input the HQL syntax and check whether the test results were correct. You can also do it in IntelliJ, but you do not need any plug-ins to provide support by default. If Hibernate support is not automatically added when importing the project, Then you need to add Hibernate support, select the project with the mouse, and right-click as shown in the figure below:
image
Click to select Hibernate. If there is no Hibernate, it should be that the project has automatically added Hibernate support. Then after completion, you can operate as shown in the figure below:
image
select the HQL icon above, and enter the HQL you want to enter in the console that comes out. It can also be done automatically.

Precautions

  1. IntelliJ saves files at any time by default, and basically does not need you ⌘+s(this key is saved by default), so you need to be careful not to accidentally change a file, and then close the label directly, in fact, your unintentional operation The file has been modified, which may cause you to find a very bizarre error when the project is running. I haven't found a setting to turn off automatic saving. If you are not obsessive about it, the vim plugin may help you. .
  2. Files in IntelliJ are synchronized with local files in real time, so don't ask about the refresh function of Eclipse, because that function is not needed here at all. Some editors will remind you whether to update with the local file. Here, no reminder is given by default. As long as you modify it, the file will be automatically synchronized and updated here.
  3. Under the main menu File, there is a strange Power Save Mode, which is also explained here, as the name implies, it is a power saving mode. When you check this mode, IntelliJ will not complete any auto-complete functions for you. For example, if you enter a character, it will be automatically prompted. After checking it, it will disappear. If this problem occurs, you can consider whether you are cheap. After clicking this button, this is more suitable for reading the code when you are bored in a meeting, and it saves power and does not affect your reading of the code.
  4. There is a function in Eclipse F2, you can view the Java file definition, you may need to configure this value in the Spring configuration file, but this is not required in IntelliJ, because the support for Spring is very complete, you only need Enter the name of the class in the class attribute and ctrl+空格 (Basic 这个在code的completion下面)the full path is automatically completed, which may not require a crappy implementation under intelligent conditions.
  5. When the project just starts importing or clearing the cache and reopening, the status bar below will display Indexing. At this time, you can just wait. Basically, you can't do anything you want. Don't be in a hurry at this time. Go, but its indexing speed is quite fast, don't worry.
  6. Eclipse has a Save Actions function, which is mainly for Java to automatically format and optimize import and other functions. This is actually not available by default in IntelliJ. If you want to use this function in IntelliJ, you can use the following two ideas:
    • Using macro recording , after the macro starts, you format the code, then optimize the import and other operations, and then end to define a macro ⌘+s, but the problem is that this macro will be executed no matter what file, not just java files, I just use this The macro mainly completes the conversion of tabs to spaces.
    • Use the plug-in of Eclipse Code Formatter . This plug-in is basically similar to the default function of Eclipse, but there are also some differences. Users can decide whether to use it.
  7. The IntelliJ code area under Mac supports gesture zooming, which is very convenient when presenting to others. However, when running, Debug and other views, the default is to manually drag the size, which is very inconvenient. You can try it ⌘+shift+up/down (Stretch to Top / Bottom). Of course, other project views, etc. can also use this shortcut key to configure the up, down, left, and right arrow keys to configure and use. It is very convenient and highly recommended.

a few tricks

  1. IntelliJ rarely sees the search box. In fact, if there is no search box, you can try to directly enter pinyin. For example, in the Project view, after opening the directory tree, you can directly type letters. If there are records that meet the conditions, they will be highlighted. to show.

finally

This is the first time to post a post. If you have any questions, please leave a message for communication. The original document is on github . If you have some skills, you may wish to improve this document together. All you need is Fock and then Pull Request. If you feel that having a github account is too troublesome, then I personally think it is more suitable for you to change careers. If you don't want to update and just want to subscribe to the continuously updated content, you only need to watch the project. You can also star the project so you can quickly find the project on your account page.

appendix

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681582&siteId=291194637