Showing posts with label Tool. Show all posts
Showing posts with label Tool. Show all posts

Another interesting MOOC for Java developer

I've just completed the EdX MOOC Data Structures and Software Design by University of Pennsylvania. I would suggest it to someone with a high-beginner or lower-intermediate experience as Java software developer. I think that the lessons would sound reasonably interesting even if your level is higher, however you could be get a bit bored by the exercises - kind of too simple. This is why I didn't push anything on my GitHub account. To have some fun, I didn't use any IDE, just solved the problems in the old way, using a dumb editor, compiling and running them from command line :D Homework 11 is, in my opinion, the most interesting one in the course. We have to refactor a poorly written, albeit working, piece of code to make it fast enough to match the requirements.

Go to the full post

A nice Java testing MOOC

I've just finished the first week of the MOOC "Automated Software Testing: Practical Skills for Java Developers" provided by TU Delft on EdX. It looks to me they did a good job. Maybe, if you are a native English speaker, the speakers' accent could sometimes sound a bit weird. However, as continental European, I kind of enjoy it (and I know that my Italian accent sounds even funnier).

The course is focused on Java, JUnit 5, using IntelliJ as IDE.

It looks like a brand new production, so be ready to step into typos and minor mistakes here and there. The most annoying of them, is the one in Question 11 on the Quizzes.

We have to write tests to detect where is the bug in a function named mirrorEnds() that sort of detect how much a string is palindromic. Unfortunately, a wrong solution has to be selected to get the point on it!

If you are taking the course, I suggest you to have a look at the discussion forum to get help.

And, if you want to compare your code with mine, please find my forked repository on GitHub.

Go to the full post

The Gradle nature of Pro Spring 5

I've started reading Pro Spring 5: An In-Depth Guide to the Spring Framework and Its Tools. It looks fun and interesting. The first issue I had was on having its code from GitHub working in my STS IDE. Here are a few notes on what I did to solve it, if someone else stumble in the same place.

First step was easy. After navigating through the File menu in this way ...
            Project from Git
                    Clone URI
I entered the repository address:

Second step should have been even easier. Add Gradle Nature to the project. It's just a matter of right-clicking on the project, and from the menu select
    Add Gradle Nature
Unfortunately, there are a few version numbers in the main "build.gradle" file for the project that are not currently working, leading to a number of annoying exceptions like:
A problem occurred evaluating project ':chapter02:hello-world'.
Could not resolve all files for configuration ':chapter02:hello-world:compile'.
Could not resolve org.springframework:spring-context:5.0.4.BUILD-SNAPSHOT.
Required by:
    project :chapter02:hello-world
Could not resolve org.springframework:spring-context:5.0.4.BUILD-SNAPSHOT.
Could not get resource ...
Could not HEAD ...
Read timed out
org.gradle.tooling.BuildException: Could not run build action ...
I tried to minimize the impact of my changes on the original configuration file.

Basically, when I had an issue, I moved to a newer version, preferring a stable RELEASE to BUILD-SNAPSHOT's or release candidates.

I also had a problem for
That I moved to
More complicated was the problem for chapter five. It looks that I have something wrong for aspectJ in my configuration. I didn't to spend too much time on that now, assuming that it would lead to to some detail in that specific area. So I opened the aspectj-aspects gradle build file and commented the dependency to gradle-aspectj and the plugin application for aspectj itself.

Now my build.gradle for chapter five - aspectj-aspects has these lines in:

    dependencies {
//      classpath "nl.eveoh:gradle-aspectj:2.0"
// apply plugin: 'aspectj'
Sure enough, it won't compile. Still, I finally added the gradle nature to the project and I can go on reading the book. I'll take care of that issue when I'll reach chapter five.

I've forked the original APress repository, and there I pushed my variants for the main build.gradle file, and the one for chapter 5, aspectj-aspects. See there for details.

Go to the full post

Packaging Spring to a jar or war

This is going to be a boring post, the flip side is that is short. I am working with Spring STS and Maven, I have already written a simple Hello World web service for Spring using Boot, and letting it use the default built-in Tomcat application server.

Let's spend some words on how to actually create the fat jar that could be run from the shell, automagically running the embedded Tomcat and the web services in it, and then how to generate instead a plain old war that could be deployed in a stand alone Tomcat.

Fat jar

The first alternative could be achieved in a click, run the Maven install goal for the project. I can either run it from the project root or, as showed below, from its pom.xml element.

The first time I ran it, I saw an annoying warning message in the log:
[WARNING] The requested profile "pom.xml" 
 could not be activated because it does not exist.
To get rid of it, I removed a spurious Maven profile from the project properties

Warning or not, Maven does its job, and I see a jar generated in the target folder, that I can run from the command line. Something like this:
...\helloSpring\target>java -jar hello-0.0.1-SNAPSHOT.jar

Classic war
If I want to get a plain old war to be deployed on an already existing application server, a few changes are required.

Java code changes

My HelloSpringApplication class should extend SpringBootServletInitializer and should override the configure method. Pay attention to the super class package, the old one, in the context.web package is now deprecated for the new one.
package dd.manny.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

public class HelloSpringApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {, args);

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloSpringApplication.class);

Configuration changes

In the POM file, the packaging element under project is now set to war, and not anymore as jar:
Then, a new dependency should be added:
And the spring-boot-maven-plugin plugin removed.

After these changes, when I run pom.xml as Maven install, I generate a war, slimmer than the original jar, that now I can deploy to my Tomcat server.

Go to the full post

A RESTful Web Service saying hello

Here I am completing the example started in the previous post, aimed at writing a simple Spring application using STS. I have ensured the app framework is working fine, now I write a very simple REST Controller.

I have already pushed the code on GitHub, however the job is so easy I don't mind to redo it from scratch. So, I create a Java class named GreetingController in the package controller under my already existing hello package
I annotate it as RestController and I provide it with a public method that returns a String, annotated as RequestMapping:
public class GreetingController {
    public String greeting() {
        return "Hello";
And that's it!

Let's see it in action. I go to the Boot Dashboard, and I start my helloSpring application.
Whoopsie daisy, I forgot to set the port of my Tomcat server to something different from the default 8080, that is already taken on this box. The result is a long scroll of complaints in the Console window, starting with an exception dump: Address already in use: bind
Ending with an error description (The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.) and the action (Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.) we should take to solve it.
There is a number of ways you could follow to solve this issue. Here I set the server port in the Spring application properties file, defined in the src/main/resources folder
When I restart Tomcat, I see I have a clean log
And now I can check on a browser how it works

Go to the full post

Hello Spring Boot

I have installed Spring STS on a Windows box, and to check that everything is OK, I am writing the usual Hello World program. I have already did it for a previous version and stored the source code on GitHub.

However, it is more fun to do it again from scratch.

From the menu File, I select the wizard to create a New Spring Starter Project.

Then I fill the required fields to specify the project name and a few extra information. I am going to keep much of the proposed default values.
Next page, I keep the default Spring Boot Version, currently 1.4.3, and I add just one extra dependency, Web.
Clicking on Next, we can check what the wizard is doing for us, using the Spring Initializr web site passing the expected parameters. Otherwise, we can simply Finish, and let STS do the work for us.

A few seconds later, I have my new shiny helloSpring project. I open the project POM (remember that I accepted the default project type, Maven), and I am not surprised to see it reflects the selections I have chosen by the wizard.
In the Spring Elements sections, there is a new bean, whose name is based on the name I gave to the project, in this case HelloSpringApplication. Let's have a look at its source code.
The class is in the dd.manny.hello package, as I asked in the wizard, and has been annotated as @SpringBootApplication. It contains a main() method that calls passing as parameters the class itself and the args passed to the application. We expect that on startup Spring sees this is the booting class, and uses its main to call the SpringApplication run() method.

To run my hello application, I now go to the Spring Boot Dashboard. By default, you should find this view on the bottom left in your STS window. If missing, you could get it back from Window - Show View
In the Boot Dashboard Local list I see my application, I select it, and now I can start the associated process. In the console window I can see the resulting logging.
Among the messages, I see a couple of them issued from my class, dd.manny.hello.HelloSpringApplication:
Starting HelloSpringApplication on a555 with PID 8552 
Started HelloSpringApplication in 1.348 seconds (JVM running for 1.995)
Good. The application worked fine.

Go to the full post