Eclipse specific development instructions - uyuni-project/uyuni GitHub Wiki

Developing with Eclipse

Install Eclipse

Install Eclipse and do not do it from packages. Instead, go to http://www.eclipse.org/ then download Eclipse IDE for Java EE Developers and install it in a directory you like (eg. /opt or ~/opt).

Test Eclipse

Add this path (eg. /opt/eclipse) to your PATH and run eclipse to test the installation. If it fails with

Unrecognized VM option 'MaxPermSize=256m'

edit /opt/eclipse/eclipse.ini and remove this line

-XX:MaxPermSize=256m

Install Eclipse IvyDE plugin

Switching back and forth between branches is much easier when using an IDE plugin for ivy, for eclipse there is IvyDE. This plugin will take care that at any time we are compiling the code against the actual dependencies of the current branch (specified via the ivy-suse.xml file).

For the plugin installation please follow the official instructions using the "Update site" approach.

Create the Eclipse project files

There are currently two java projects that can be automatically imported to Eclipse.

  • This procedure will also work for Intellij IDEA (it can import Eclipse projects directly).

spacewalk-java

cd <path_to_spacewalk>/java
ant make-eclipse-project

After this step you will have your .classpath file with JUnit and Ivy libraries. Note that the IvyDE plugin is now required

Next, import it to the Eclipse workspace:

  • Go to File -> Import in your Eclipse.

  • Choose General -> Existing Projects into Workspace

  • Select root directory to <path_to_spacewalk>/java

  • Click "Finish"

In build path, add java/lib/java-branding.jar as JAR dependency.

Wait until project get refreshed and look for the Errors, if any (you are supposed to have none).

Happy coding. :-)

spacewalk-search

cd <path_to_spacewalk>/search-server/spacewalk-search
ant make-eclipse-project

Next, follow the same import procedure as define above for spacewalk-java (adjusting the directories).

Faster deployment

If you use Eclipse chances are that sources get automatically compiled every time you make a change; the Ant build scripts provides a way of re-using those files to avoid calling javac at every deploy.

You should:

  • ensure that your IDE is outputting bytecode in a format that is compatible with the JRE used on the deploy target host (Java 6 for Manager 1.7, Java 7 for 2.1 and Java 8 for 3). In Eclipse, you can use any JRE (see "Java Build Path" in the project Properties) as long as you set the compiler compliance level accordingly (see "Java Compiler" in the project Properties);
  • ensure that precompiled files go into build/classes. In Eclipse, this is done via the "Default output folder" field in the "Java Build Path" option within the project Preferences window;
  • enable precompiled file use by adding a precompiled=true line in buildconf/manager-developer-build.properties (e.g. make a copy of buildconf/manager-developer-build.properties.example, then rename and edit the new file).

Automatic code formatting

Most formatting is done via an explicit command because it is not always enforced. You should definitely apply it on any new code, unless it breaks Checkstyle rules.

Use:

  • select the portion of Java code to format;
  • press Shift + Ctrl + F or right-click and select Source -> Format;

There should be no need to update the configuration since this is now created by the ant target mentioned above. For the record here is how to do it otherwise:

Configure:

  • click on Window -> Preferences -> Java -> Code Style -> Formatter -> Import...;
  • select conf/eclipse/code_formatter_rhn.xml;

Some formatting is enforced basically everywhere, so it can be configured to be automatically fixed on save.

Configure:

  • click on Window -> Preferences -> Java -> Editor -> Save Actions;
  • select only Additional actions and click Configure;
  • select only Remove trailing whitespace (from All lines) from the Code Organizing tab.

Automatic import reorganizing

Again this is done on a case-by-case basis but should be done for all new code.

Use:

  • select the portion of Java code to organize;
  • press Shift + Ctrl + O or right-click and select Source -> Organize Imports;

There should be no need to update the configuration since this is now created by the ant target mentioned above. For the record here is how to do it otherwise:

Configure:

  • click on Window -> Preferences -> Java -> Code Style -> Organize Imports -> Import...;
  • select conf/eclipse/uyuni.importorder;

Automatic code cleanup

Cleanup does reformatting, import organizing, reindentation and more. Usually more aggressive than the above, yet this could be a benefit in some cases!

There should be no need to update the configuration since this is now created by the ant target mentioned above. For the record here is how to do it otherwise:

Configure:

  • click on Window -> Preferences -> Java -> Code Style -> Clean-up -> Import...;
  • select conf/eclipse/cleanup_preferences.xml;
  • select the portion of Java code to clean up;
  • right-click and select Source -> Clean Up...;

Code and comments templates

You can get templates for stubbing out code in a standards-and-Checkstyle compliant way. There should be no need to update the configuration since this is now created by the ant target mentioned above. For the record here is how to do it otherwise:

  • click on Window -> Preferences -> Java -> Code Style -> Code Templated -> Import...;
  • select conf/eclipse/code_templates_code.xml;
  • repeat selecting conf/eclipse/code_templates_comments.xml;

Autogenerating Javadoc (with a clue)

Most Javadoc documentation can be generated automatically via the JAutoDoc plugin.

  • install it by clicking on Help -> Install New Software... -> Add... (a Software Site):
    • Name: JAutoDoc
    • Location: http://jautodoc.sourceforge.net/update/
  • select the feature and follow instructions till the end;
  • configure it by clicking on Window -> Preferences -> JAutoDoc -> Import All... and selecting conf/eclipse/jautodoc_preferences.xml;

Inline continuous Checkstyle checking

Most Checkstyle problems can be caught before commit via the Checkstyle plugin.

  • install it by clicking on Help -> Install New Software... -> Add... (a Software Site):
    • Name: Checkstyle
    • Location: http://eclipse-cs.sf.net/update/
  • select the feature and follow instructions till the end;
  • configure it by clicking on Window -> Preferences -> Checkstyle -> New (internal configuration) -> Setting Uyuni as name, clicking Import... and selecting conf/eclipse/checkstyle_eclipse.xml;

Note that the name is important since it is used in the configuration generated by the ant target initializing the Eclipse project

Remote debugging

Use vagrant ports to get the debugging port to connect to (or set it manually if you are not using suminator).

Start a debug session in Eclipse. Put a breakpoint in an interesting location, then Debug as a Java Remote Application setting the Connection Properties appropriately.

Remote debugging of Taskomatic tasks

  • Edit /usr/share/rhn/config-defaults/rhn_taskomatic_daemon.conf, add the lines:

      wrapper.java.additional.XXX=-Xdebug
      wrapper.java.additional.XXX=-Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n
    
  • run rctaskomatic restart

  • connect with Eclipse as usual.

Note: XXX should reflect usual parameter numbering.

Run or debug JUnit tests in Eclipse

First, follow general instructions to set up the tests and ensure they work.

Then you can also use the integrated Eclipse JUnit runner (optionally with debug support) by doing the following:

  • click on Run, Run Configurations... and select JUnit from the left panel;

  • click on the New button (in the upper left corner);

  • select Run a single test and point to the correct class;

  • select the JUnit 3 test runner;

  • in the Arguments tab, add the following to the VM Arguments box:

      -Drhn.config.dir=${project_loc}/buildconf/test/ -Dlog4j.threshold=debug 
    
  • click on Run.

Show the log in the IDE console when running tests from the IDE

To get the log messages to be displayed in the IDE console when launching a unit test from the IDE do the following:

  • create a file in some location, e.g. spacewalk/java/buildconf/test/log4j.properties similar to this:

      log4j.rootLogger=DEBUG,CONSOLE
    
      log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
      log4j.appender.CONSOLE.Target=System.out
      log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
      log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
    
  • follow the steps on how to run a unit test from the IDE but add this system property:

      -Dlog4j.configuration=${project_loc}/buildconf/test/log4j.properties
    

Hint: for Intellij you can also use -Dlog4j.configuration=file:///$MODULE_DIR$/buildconf/test/log4j.properties