📄 Page
1
www.it-ebooks.info
📄 Page
2
Getting Started with IntelliJ IDEA Exploit IntelliJ IDEA’s unique features to rapidly develop web and Java Enterprise applications Hudson Orsine Assumpção BIRMINGHAM - MUMBAI www.it-ebooks.info
📄 Page
3
Getting Started with IntelliJ IDEA Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: October 2013 Production Reference: 1101013 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84969-961-7 www.packtpub.com Cover Image by Abhishek Pandey (abhishek.pandey1210@gmail.com) www.it-ebooks.info
📄 Page
4
Credits Author Hudson Orsine Assumpção Reviewers Scott Battaglia Tomasz Nurkiewicz Acquisition Editors Edward Gordon Rubal Kaur Lead Technical Editor Mohammed Fahad Technical Editors Adrian Raposo Gaurav Thingalaya Copy Editor Tanvi Gaitonde Project Coordinator Romal Karani Proofreader Amy Johnson Indexer Rekha Nair Production Coordinator Kirtee Shingan Cover Work Kirtee Shingan www.it-ebooks.info
📄 Page
5
About the Author Hudson Orsine Assumpção is a Brazilian software engineer with a bachelor’s degree in Information Systems from Universidade Estadual de Montes Claros (Unimontes). He has experience in developing applications in Java EE and ActionScript 3. He also has practical knowledge in Data Warehouse, OLAP tools, and BPMN. He is a certified ITIL V3 foundation professional. Currently, he is developing, with a friend, a web-based ERP system using the Java EE platform. I would like to first thank God, who is present all the days in my life, my family for their support, and a special thanks to my friend Rayldo, who encouraged me while writing this book and helped me organize my time in a way that I could do my job as well as write this book. Thanks to all friends in Packt Publishing; you really helped me organize and write the content of this book. www.it-ebooks.info
📄 Page
6
About the Reviewers Scott Battaglia is a senior Software Development Engineer for Audible.com, the leading provider of premium digital spoken audio information, currently focused on Android development. Prior to that, he was an Identity Management Architect and senior Application Developer with Rutgers, The State University of New Jersey. He actively contributes to various open source projects, including Apereo Central Authentication Service and Inspektr, and has previously contributed to Spring Security, Apereo OpenRegistry, and Apereo uPortal. He has spoken at various conferences, including Jasig, EDUCAUSE, and Spring Forward, on topics such as CAS, Identity Management, Spring Security, and software development practices. Scott holds a Bachelor of Science and Master of Science degree in Computer Science from Rutgers University and a joint Master of Business Administration and Master of Public Health—Health Systems and Policy—degree from Rutgers University and the University of Medicine and Dentistry—School of Public Health. Scott is on the advisory board for Blanco’s Kids, a group focused on providing public health services to extremely poor communities in the Dominican Republic. In his spare time, he enjoys photography, running marathons, learning new programming languages, and sponsoring random Kickstarter projects. Tomasz Nurkiewicz is a Software Engineer with 7 years of experience, mostly developing backend with JVM languages. He is a Scala enthusiast. Presently, he is working on leveraging the power of functional programming in the global banking industry. Tomasz strongly believes in automated testing at every level. He claims that a functionality not tested automatically is not trustworthy and will eventually break. He is always happy to implement monitoring and data visualization solutions and has reviewed the book Learning Highcharts, Packt Publishing in the past. He is a data analysis apprentice and a technical blogger (http://nurkiewicz.blogspot.com), speaker at conferences, and trainer. Most importantly, he has been a proud user of IntelliJ IDEA for many years. www.it-ebooks.info
📄 Page
7
www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt’s online digital book library. Here, you can access, read and search across Packt’s entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info
📄 Page
8
Table of Contents Preface 1 Chapter 1: Getting Started with IntelliJ IDEA 12 5 Presenting features 5 Smart code completion 6 On-the-fly code analysis 7 Advanced refactoring 7 Navigation and search 9 Tools and frameworks support 10 What is new 11 Installing 12 Configuring 15 Project Structure 16 Virtual machine options 17 Migrating from another IDE 18 What to expect in the near future 18 Summary 19 Chapter 2: Improving Your Development Speed 21 Identifying and understanding window elements 22 Identifying and understanding code editor elements 24 Doing things faster 26 The productivity guide 26 Navigating your source code 27 Using code facilities 29 Live templates 30 Using refactoring techniques 32 Managing your changes 34 Organizing your activities 35 Tasks and context management 35 www.it-ebooks.info
📄 Page
9
Table of Contents [ ii ] Using TODO marking 37 Plugins 38 JRebel 39 Hungry Backspace 40 Key promoter 40 GenerateTestCase 41 Summary 42 Chapter 3: Working with Databases 43 Database tool 43 Creating the database 44 Connecting to the database 44 Creating tables 47 Manipulating data 50 ORM Support 53 Creating database entities 54 Problems that can occur 56 Summary 57 Chapter 4: Web Development 59 Creating a web module 59 Configuring the application server 60 Developing our application 64 Configuring the JSF environment 64 Resolving the dependencies 65 Creating the filter code 68 Final adjustments 72 Creating SOAP web services 75 Creating test code 76 Finalizing the web service code 79 Summary 81 Chapter 5: Desktop Development 83 Creating the desktop application 83 Discovering the visual editor 84 Creating the web service client 89 Data binding 91 Migrating GUI 95 Summary 97 Index 99 www.it-ebooks.info
📄 Page
10
Preface Developing codes that are correct in a fast manner is a difficult task, even for senior developers. In conjunction with the abilities of the developer, great tools are involved in providing a simple, flexible, and fast way of developing software. IntelliJ IDEA is one of the most powerful IDEs for Java developers, extending facilities you can find in common IDEs and providing features you’ve probably never seen before. In this this book, you will learn how to extract the maximum from IntelliJ IDEA 12, beginning with understanding the basic concepts and applying features in real development challenges. You will see your development speed improve naturally and understand why IntelliJ IDEA is considered to be the smartest IDE in the world. What this book covers Chapter 1, Getting Started with IntelliJ IDEA 12, presents some features in a superficial way. The intention here, is to show you the features you can expect in IntelliJ IDEA. The chapter begins by showing common features, then it shows you what is new in this version of the IDE. You will also see the differences between the Community Edition and Ultimate versions, and how to install and configure this software. At the end of the chapter is a section where you will see what you can expect in the future versions of IntelliJ IDEA. Chapter 2, Improving Your Development Speed, will provide a wide range of functionalities that will improve your development speed. In the beginning, it presents to you the main visual interface of the IDE. After this, it shows you how to improve your productivity using the features of IntelliJ IDEA such as the productivity guide, live templates, and navigation usabilities. More development speed techniques are discussed later in the chapter, such as organizing tasks using your favorite issue tracker and how to use TODO marks. At the end of the chapter, a small list of diverse plugins is presented. www.it-ebooks.info
📄 Page
11
Preface [ 2 ] Chapter 3, Working with Databases, explores diverse database facilities. Different from the previous two, this chapter uses a practical approach to show the features of IntelliJ IDEA that could make your work with databases even simpler. This chapter begins by showing the Database Tool; here, we will create a simple database using this tool. We will use visual elements to create, edit, and populate a table, and visualize data in a table and filter it. In the end, we will understand how to work with ORM technologies, such as Hibernate, extracting entities from the database we created. Chapter 4, Web Development, continues using a practical approach to develop a simple web application. In this chapter, some features that are available in all kinds of projects are used, such as refactoring techniques. We start this chapter by creating a web module and configuring the application server. After this is done, we configure the web module to use the frameworks we need such as JSF and Spring. Then, we correct the dependencies of the module using IntelliJ IDEA facilities and develop our code. At the end of the chapter, we develop a simple web service that will be used in the next chapter. Chapter 5, Desktop Development, will show you how to develop a simple desktop application. In the beginning, we will create a desktop application module and discover the visual editor. While we explore this tool, we will create the GUI of our application and see how the visual editor is integrated with the whole IDE. Then, we will consume the web service we created in the previous chapter and, finally, configure the data binding of our application. What you need for this book In some chapters in this book, we will use some external software that needs to be installed in your computer; these are as follows: • MySQL version 5.5 • Apache Tomcat 7 • We also need Java Development Kit (JDK) 7 or later. Who this book is for If you are a developer, experienced or not, and want to improve your development speed or just understand what IntelliJ IDEA can offer you, this book is directed to you. You just need basic background knowledge of Java development to follow this book; however, some technologies used in this book can be advanced and will not be explained in depth. www.it-ebooks.info
📄 Page
12
Preface [ 3 ] Conventions In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning. Code words in text are shown as follows: "Mount the downloaded .dmg file as another disk." A block of code is set as follows: private void showMessage(Status status) { //TODO: something should be done here. } Any command-line input or output is written as follows: mysql -u root -p New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: " In this initial window, you can open the Settings window". Warnings or important notes appear in a box like this. Tips and tricks appear like this. Reader feedback Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of. To send us general feedback, simply send an e-mail to feedback@packtpub.com, and mention the book title through the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors. www.it-ebooks.info
📄 Page
13
Preface [ 4 ] Customer support Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase. Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. Errata Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www. packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title. Piracy Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at copyright@packtpub.com with a link to the suspected pirated material. We appreciate your help in protecting our authors, and our ability to bring you valuable content. Questions You can contact us at questions@packtpub.com if you are having a problem with any aspect of the book, and we will do our best to address it. www.it-ebooks.info
📄 Page
14
Getting Started with IntelliJ IDEA 12 This introductory chapter will present some features of IntelliJ IDEA (later referred to simply as IntelliJ) and how you can install and configure it. You will be presented with some of the great features of IntelliJ IDEA and will notice for yourself that, even if you've already seen some of the features presented here in other IDEs, none of them are so focused to provide facilities to the work of the developer. After you've seen the features, you will learn how to install and configure IntelliJ. Despite these being really simple tasks, you will probably see great value in this information, mainly because the organization and nomenclature of some elements may differ from that of the IDE you used before. So, what is provided here is to help you remove some barriers you may encounter while using IntelliJ. Presenting features As the name suggests, IntelliJ IDEA tries to be an intelligent Integrated Development Environment (IDE). To do this, it applies techniques that permit the automation of some tasks and suggests actions that may facilitate the developer's work. Allied with this, IntelliJ provides out-of-the-box integration with popular frameworks used by Java developers. This native integration drastically reduces the existence of bugs and provides great synergy between the different technologies used to construct your project. Here, I will show you some of the great features that exist in IntelliJ IDEA 12. www.it-ebooks.info
📄 Page
15
Getting Started with IntelliJ IDEA 12 [ 6 ] Smart code completion Code completion is a set of techniques used to analyze and propose pieces of code to the developer. Like other IDE, IntelliJ also presents code completion; but, as compared to other techniques, code completion in IntelliJ is much smarter. • It can detect and differentiate between a big quantity of languages, such as Java, Scala (using a plugin), Groovy, SQL, JPQL, JavaScript, and so on, even when they are injected in another sentence as shown in the following screenshot (for example, a SQL string in Java code): • Beyond suggested variables, classes, and method names, it can suggest methods, parameters, properties, filenames, resources, Spring Framework's beans, database tables, and so on, even when you are working with non-Java files. • Smart code completion suggests sentences based on the context and the user needs. So it can suggest, for example, chain completion like getModule().getProject() that fit correctly in the context as shown in the following screenshot: • IntelliJ can propose completion based on middle name search and camel case abbreviation as shown in the following screenshot: www.it-ebooks.info
📄 Page
16
Chapter 1 [ 7 ] On-the-fly code analysis An IDE isn't useful if it can't identify whether or not an expression is invalid and will bring compilation errors to the developer. Instead of only identifying these problems, IntelliJ tries to help you improve your code. While typing your source code, you will notice that IntelliJ will analyze your code in the background. In these analyses, IntelliJ will use its configurations to identify errors and possible improvements. The kind of analyses that IntelliJ does in the code are fully configurable; these configurations are called inspections and are one of the most powerful features of this IDE. With inspections, IntelliJ can find a large range of existing errors, possible bugs, or improvements, such as identifying if a variable is never used, suggesting a simplified sentence, identifying thread problems, and more, as you can see in the following screenshot: Advanced refactoring Sometimes, the refactoring process is very tiring because you need to make a lot of modifications to a lot of files. Even when you do the refactoring carefully, sometimes compile errors appear, for example, because you forgot to change a method usage. Fortunately, IntelliJ can help us do these tasks. www.it-ebooks.info
📄 Page
17
Getting Started with IntelliJ IDEA 12 [ 8 ] IntelliJ has a big range of refactoring options that help the developer refactor his/her code faster and in a more secure manner. Depending on the section of code you are working on, IntelliJ will automatically suggest the refactoring. In case it doesn't propose any refactoring or gives you an option you don't want to use, you can select an option from the refactor menu. The following are some of the refactoring options available: • Type migration: With this, the refactoring technique can be useful in changing a member type in a safe way because IntelliJ will find and modify all the usages of the member. You can use this refactoring in a bunch of situations, such as in method return type, local variable, and parameter. • Extract class: This refactoring option enables the developer to extract one class from another. The developer can choose which methods and variables he/she wants to transfer to the new class. This is very useful when you have a very huge and complex class and want to simplify it. There is a similar option that creates a superclass based in the current class being edited and another that extracts an interface in the same way. • XML-aware dedicated refactoring: There are more than 50 refactoring options exclusive to XML files. For example, you can rename tags and attributes, wrap and unwrap tags, replace attributes with tags and vice versa, convert tags to attributes, safely add and remove subtags and attributes, and so on. If you use some framework that works with XML files, such as Spring, you will see that you can, for example, refactor a bean parameter's name directly in XML. • Drag-and-drop items: Sometimes you look at your project tree and think that it would be better to move some classes and packages. In IntelliJ, you can drag-and-drop the elements from one place to another in your project, and it will automatically change the imports and usages for you. • Change method signature: You have probably already experienced a situation like this: you created a method and, after doing a lot of work, you've decided to change the method signature. Using IntelliJ, it is possible to minimize the effects of a method signature change. IntelliJ can remove, reorder, and add parameters to the signature; it is also possible to change the return type and the parameter types. In case you are adding a parameter, IntelliJ will insert a default value in all the usages of the method. www.it-ebooks.info
📄 Page
18
Chapter 1 [ 9 ] • Introduce constant: Sometimes, while developing, we don't see that it is possible to use a constant instead of repeating the same value all throughout the class. You can easily create a constant based on the repeated values present in your code. Once the constant is created, IntelliJ will detect the entries and replace them as shown in the following screenshot: Navigation and search One of the great characteristics you will find in IntelliJ is the ease of finding and navigating to a resource. You can run a search in practically all the controls that exist in the IDE, even when you can't see a search field. You can also use different elements to navigate to a specific code that you want. The following features can be highlighted to navigate through the source code: • A dialog that shows the opened files and IDE windows can be used to open the desired element without using the mouse • You can navigate to a declaration from a usage • You can navigate to a usage from a declaration • A breadcrumb helps to access files that are in the current path or someplace near the actually used file, as shown in the following screenshot: www.it-ebooks.info
📄 Page
19
Getting Started with IntelliJ IDEA 12 [ 10 ] • Small symbols are available on the left side of the code editor for navigation. Clicking on these icons will focus the cursor in the desired code as shown in the following screenshot: • It is possible to jump to the last change the developer did, no matter if the file is in the current module or closed For running a search, the following features can be highlighted: • You can find the usages of a piece of code by just clicking on one menu • Structural searches can be used to find and replace sentences that can't be represented by a simple string pattern, such as finding only the values of field initializers • It is possible to find elements in places where you can't see any search field, for example, you can search for a class in the Project Structure tree • You can use a regular expression for find and replace Tools and frameworks support IntelliJ provides support for different tools that help you to develop your application using it. The integration between the tools and the IDE helps developers to focus and reduces some reworks, mainly because the changes that are made by one tool reflect in another tool and, sometimes, starts a refactoring process. A lot of frameworks have advanced support in IntelliJ; to cite some of them: Spring, Play, Grails, JSF, Struts, Flex, JPA, Hibernate, and so on. The support for these frameworks, in conjunction with the integration of different tools, makes the development very simple and productive. • IntelliJ has a Unified Modeling Language (UML) tool that you can use to create your classes or just to see how your classes are organized. The UML tool uses refactoring techniques, so you can change a class in the UML editor and be secure that the code will be changed too. www.it-ebooks.info
📄 Page
20
Chapter 1 [ 11 ] • Basically, you won't need to use another program to create databases, tables, keys, and even to store procedures. No matter which database you are using, IntelliJ will guess the correct dialect for the database; in the rare case it doesn't choose well, you can configure the SQL dialect of the project, file, or directory. There is a powerful graphical table editor available, so you can do the majority of your work in a visual environment, as shown in the following screenshot: • IntelliJ supports the most known Version Control Systems (VCS) such as Git, subversion, CVS, Mercurial, and more. With this tool the user can work with different VCSs in a similar way. You can view the differences, changes, and history, manage change lists, integrate with your favorite bug tracker, and more. • IntelliJ is also fully integrated with the following most commonly used build tools: Maven, Ant, Gradle, and Gant. They provide good integration for unit tests and not only to Java code, but also to JavaScript, Flex, Android, PHP, and others too. What is new The Version 12 of IntelliJ comes with good enhancements and new features, such as the following: • JavaFX 2: Now it is possible to use code completion, navigation, refactoring, and CSS facilities in JavaFX 2 projects • Java 8: In this version, the next generation of the Java platform is fully supported with code assistance for the new syntax, such as Lambda expressions, type annotations, default methods, and method references www.it-ebooks.info