Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, June 10, 2012

Eclipse Shortcuts

Eclipse Shortcuts:


Ctrl+Shift+T

Find Java Type

Start typing the name and the list gets smaller. Try typing the capital letters of the class only (e.g. type "CME" to find "ConcurrentModificationException")

Ctrl+Shift+R

Find Resource

Use this to look for XML files, text files, or files of any other type. which are in your workspace.

Ctrl+E

Open Editor Drop-Down

Presents a popup window listing currently opened files. Start typing to limit the list or simply use the down arrow key.

Ctrl+O

Quick Outline

Use this to find a method or a member variable in a class. Start typing to limit the choices. Press Ctrl+O a second time to include inherited methods.

Ctrl+Space

Content Assist

Context sensitive content completion suggestions while editing Java code.

Ctrl+Shift+Space

Context Information

If typing a method call with several parameters use this to show the applicable parameter types. The current parameter where the cursor is will be shown in bold.

Ctrl+Shift+O

Organize Imports

After typing a class name use this shortcut to insert an import statement. This works if multiple class names haven't been imported too.

F3

Open Declaration

Drills down to the declaration of the type, method, or variable the cursor is on. This works much like a browser hyperlink.

Alt+Left

Backward History

This works like a browser's Back button.

Alt+Right

Forward History

This works like a browser's Forward button

Ctrl+L

Go to Line

Go to a specific line number.

F4

Open Type Hierarchy

Show the type hierarchy (downward tree) or the supertype hierarchy (upward tree).

Ctrl+Alt+H

Open Call Hierarchy

Show where a method is called from. In the Call Hierarchy view keep expanding the tree to continue tracing the call chain.

Ctrl+H

Open Search Dialog

Opens a search dialog with extensive search options for Java packages, types, methods, and fields.

Alt+Shift+R

Rename - Refactoring

Use this to rename type, method, or field. All existing references will be refactored as well.

Alt+Shift+L

Extract Local Variable

Use this to create a local variable from the selected expression. This is useful for breaking up larger expressions to avoid long lines.

Alt+Shift+M

Extract Method

Use this to extract a new method from existing code. The parameter list and return type will be automatically created.

Alt+Shift+Up
Alt+Shift+Down
Alt+Shift+Left
Alt+Shift+Right


Select Enclosing Element / Restore Last Selection / Select Previous Element /
Select Next Element


Useful for selecting context-sensitive blocks (e.g. surrounding loop, method, class, etc.)

Ctrl+Up
Ctrl+Down


Scroll Line Up / Scroll Line Down

Very handy if you want to scroll by 1 line without changing your cursor position or using the mouse.

Ctrl+Shift+U
Alt+Shift+U


Go to Previous Member / Go to Next Member

Great for stepping down through the methods of a Java source file.

Ctrl+Shift+U
Alt+Shift+U


Show Occurrences in File / Remove Occurrences Annotations

Use this to search within the same file - useful for occurrences of private fields and methods.

Ctrl+Shift+P

Go to Matching Bracket

Helps to find the closing bracket of lengthly if-else statements.

Ctrl+J
Ctrl+Shift+J


Incremental Find / Reverse Incremental Find

The first matching occurrence is shown with each typed letter. Press again and the next matching occurrence is shown.

Shift+Enter
Ctrl+Shift+Enter


Insert Line Below / Insert Line Above

Insert a line above or below the current line.

Ctrl+/
Ctrl+\


Add Block Comment / Remove Block Comment

Comment in/out blocks of code with a key stroke.

Ctrl+M

Maximize Active View or Editor

Maximize the current view or editor at the expense of all other currently shown views. Press again to restore to normal view.

Ctrl+F6
Ctrl+F7
Ctrl+F8


Next Editor / Next View / Next Perspective

Learn these to switch among edited files, open views and perspectives.

Ctrl+Alt+Up
Ctrl+Alt+Down


Duplicate Lines / Copy Lines

Doesn't seem like it at first but a great shortcut once you learn to use it. Instead of select, copy and paste simply select and duplicate without affecting the clipboard.

Alt+/

Word Completion

This is excellent for code editing or writing plain help files with variables and other words having no English language equivalents. The word completion is based on the set of words already present in the current file.

Ctrl+I

Correct Indentation

Select a block of Java code or an entire class file and use this shortcut to correct its indentation.

Tuesday, October 11, 2011

Maven for Java Application's

Maven for java

Maven is a Java tool, so you must have Java installed in order to proceed.

First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:
mvn --version
It should print out your installed version of Maven, for example:
Maven version: 2.0.8
Java version: 1.5.0_12
OS name: "windows 2003" version: "5.2" arch: "x86" Family: "windows"
Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.

Creating a Project

On your command line, execute the following Maven goal:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don't worry, there are ways to fix that.
You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
cd my-app
Under this directory you will notice the following standard project structure.
my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml is the project's Project Object Model, or POM.

The POM

The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

What did I just do?

You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This goal created a simple project based upon an archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".

Build the Project

mvn package
The command line will print out various actions, and end with the following:
 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Oct 05 21:16:04 CDT 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:
1.    validate
2.    generate-sources
3.    process-sources
4.    generate-resources
5.    process-resources
6.    compile
You may test the newly compiled and packaged JAR with the following command:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Which will print the quintessential:
Hello World!

Running Maven Tools

Maven Phases

Although hardly a comprehensive list, these are the most common default lifecycle phases executed.
  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
There are two other Maven lifecycles of note beyond the default list above. They are
  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war is the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.
mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).

Generating the Site

mvn site
This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.

Conclusion

We hope this quick overview has piqued your interest in the versitility of Maven. Note that this is a very truncated quick-start guide. Now you are ready for more comprehensive details concerning the actions you have just performed. Check out the Maven Getting Started Guide.