Monday, December 12, 2016

Simple Maven and Jetty Application

Today I have successfully developed a simple Java Application that uses Maven and Jetty.

The project was called "MaveJettyApp“ and the file structure was the following:

The pom.xml file:

    4.0.0
    com.zygimantus
    MavenJettyApp
    war
    1.0-SNAPSHOT
    
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
    
    
        
            
                org.mortbay.jetty
                jetty-maven-plugin
                8.1.16.v20140903
            
        
    

The MainServlet.java file:
package com.zygimantus;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {""})
public class MainServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.getWriter().write("Hello World!");
        } catch (IOException e) {
        }
    }
}

In this example Jetty is used as Maven plugin. Using the command mvn jetty:run runs the server. And after opening http://localhost:8080/ in a browser a successful message "Hello World" was shown.

Full source code can be found here: https://gist.github.com/zygimantus/104ee4cba0571388234dab989b86cba0

Monday, October 17, 2016

Daily Git commands

Here are some of Git commands that I am using daily:
# removes last commit
git reset --hard HEAD~1
# shows branches with last commits
git branch -v
# show commits in current branch
git log --oneline
# shows information about remote repo
git remote show origin

Useful Maven plugins

Today I would like to share some useful Maven plugins:
  • exec-maven-plugin
    • executes JAR with mvn exec:java command
  • maven-jar-plugin
    • builds an executable JAR
  • maven-dependency-plugin
    • adds dependencies to lib/ folder
  • maven-assembly-plugin
    • includes dependencies to jar

    org.codehaus.mojo
    exec-maven-plugin
    1.1
    
        com.package.Main
    


    org.apache.maven.plugins
    maven-jar-plugin
    2.4
    
        
            
                true
                lib/
                com.package.Main
            
        
    


    org.apache.maven.plugins
    maven-dependency-plugin
    
        
            copy
            package
            
                copy-dependencies
            
            
                
                    ${project.build.directory}/lib
                
            
        
    


    maven-assembly-plugin
    
        
            package
            
                single
            
        
    
    
        
            jar-with-dependencies
        
        
            
                ${mainClass}