Pages

Showing posts with label commandline. Show all posts
Showing posts with label commandline. Show all posts

Friday, September 23, 2016

Make your GitHub/Git (java) project great again with cheats

  Following blog-post is fully dedicated to GitHub and Git itself. I'll definitely use it as cheat table any time I forget any specific command because cheat tables makes our work faster and free up our memory space.
 Most of time I do use SourceTree for Mac which is pretty stable but sometimes I do have only command line with tmux. So I need to write commands by myself here are cheat tables:

CONFIGURE STUFF
$git config --global user.name "[magic name]"
$git config --global user.email "[super email]"
$git config --global color.ui auto 
I think all is almost self explanatory and coments are note necessary 

CREATE REP.
$git init [your-project-name]
$git clone [project-url]

MAKE GREAT CHANGES
$git status
$git diff
$git diff --staged
$git add [cool/not cool files]
$git reset [only not cool files:)]
$git commit -m "[please inform]"

DO GROUP CHANGES...
$git branch 
$git branch [desired-branch-name]
$git branch -d [branch-you-dont-want]
$git checkout [some-branch-you-want]
$git merge [a-lot-fun-branch]

TIME TO REFACTOR FILES
$git rm [file-I-dont-want]
$git rm --cached [file-to-remove-but-stay-locally]
$git mv [where-file-is] [here-file-goes]

HISTORY COUNTS
$git log
$git log --follow [my-favorit-file]
$git diff [branch-I-ve-worked-on1]...[another-branch-some-else-worked-one]
$git show [magic-commit]

CHANGE (REDO) COMMITS
$git reset [moving-from-corrupted-branch-to-ok-branch]
$git reset --hard [please-move-me-into-this-branch]

SAVE FRAGMENTS
$git stash
$git stash pop
$git stash list
$git stash drop

ALL CHANGES ARE IMPORTANT...
$git fetch [favorite-bookmark]
$git merge [bookmark-I-need]/[magic-branch]
$git push [alias] [favorite-branch]
$git pull

  And it's not all but enough to make a great GIT job great again! 

Sunday, August 21, 2016

Java 8: How to create executable fatJAR withouch IDE :: command-line : explained

  The executable fatJAR (fat Java ARchive) is pretty handy thing. Besides the fact that it can be quite big, it contains all necessary libraries to execute the developed program. 
The JAR file is based on ZIP format.
  In the previous posts:
a.  "Java 8 : How to create executable JAR without IDE, command-line :: explained"
b.  "Java 8 : How to create executable JAR without IDE with packages, command-line :: explained" 
we have explained how we handle pure JAVA project without any external libraries with or without complicated package structure. 
  The goal of current post is to create fatJAR file. The JAR which contains all necessary external libraries. Let's start slowly by creating new pure java project over the command line. We call it 
"Executable-Two" (GitHub)
  The executable-two project has following folder structure:
./ExecutableTwo.jar
./libs
./libs/ExecutableOne.jar
./out
./README.md
./src
./src/main
./src/main/java
./src/main/java/com
./src/main/java/com/exec
./src/main/java/com/exec/two
./src/main/java/com/exec/two/Main.java
./src/main/resources
./src/main/resources/META-INF
./src/main/resources/META-INF/MANIFEST.MF
  The folder LIBS contains previously created JAR file "ExecutableOne.jar". 
The "ExecutableOne.jar" contains the MagicService class which we will use inside the project "ExecutableTwo". We will instantiate the class MagicService and execute the public method getMessage(). All this will happen inside the Main class of the project "ExecutableTwo". Let's create following Main class in the project 
package com.exec.two :
1 package com.exec.two;                                                                                                                                                                  
2                                                                                                                                                                                        
3 import com.exec.one.service.MagicService;                                                                                                                                              
4                                                                                                                                                                                        
5 public class Main {                                                                                                                                                                    
6                                                                                                                                                                                        
7     public static void main(String[] args){                                                                                                                                            
8                                                                                                                                                                                        
9         System.out.println("Executable-Two Main");                                                                                                                                     
10         MagicService service = new MagicService();                                                                                                                                     
11         System.out.println("MagicService from Executable-ONE");                                                                                                                        
12         System.out.println("MESSAGE: " + service.getMessage());                                                                                                                        
13                                                                                                                                                                                        
14     }                                                                                                                                                                                  
15                                                                                                                                                                                        
16 }
   Now we have everything ready for fatJAR file creation. We have imported MagicService from the jar library we have previously created and we have executed it's getMessage() method. 
For the next steps we will use javac and jar tools provided by Java JDK. Let's open the command line and compile the project. In the command we need to inform the compiler that we should extend its classpath about the used library.
$javac -cp ./src/main/java 
./src/main/java/com/exec/two/*.java -d ./out/ 
-classpath ./libs/ExecutableOne.jar
  The "Executable-Two" project has been successfully compiled into the OUT directory.
Now it's time to properly prepare OUT directory for fatJAR creation. Inside the directory are available only compiled class we have created for project "Executable-Two". We will use again for JAR file creation jar tool, but the problem is that jar tool reads only the file physically located on the filesystem. The jar tool won't read compressed jar file. 
It means even when we copy the ExecutableOne.jar into the OUT directory jar tool will not unpack the ExecutableOne.jar file and the library will be add to the result but ignored.

   The problem is that $java -jar tool does not read inner packaged *.jar archive files!  

  It implies that we need to unpack previously created Java Archive (JAR) "Executable-One.jar" into the "Executable-Two" project OUT directory.
Open the command line and type: 
$cp libs/ExecutableOne.jar ./out/
$cd ./out
$tar xf ExecutableOne.jar
$rm ExecutableOne.jar 
  Used JAR tool command options: x - extract, f - file
  Now  "Executable-Two" project output directory OUT is ready to be used as the source folder for the new JAR file.
NOTE: inside every executable jar file is only one MANIFES.FM file available.
  For packing our "Executable-Two" project into the JAR archive file we do use newly created manifest file located in the folder ./src/main/resources/META-INF/ :
1 Manifest-Version: 1.0                                                                                                                                                                  
2 Class-Path: .                                                                                                                                                                          
3 Main-Class: com.exec.two.Main   
and now we can pack all together by typing:
$jar cvfm ExecutableTwo.jar ./src/main/resources/META-INF/MANIFEST.FM -C./out/ .
  When we executed the created fatJAR file we received following output:
$java -jar ./ExecutableTwo.jar
output:
Executable-Two Main
MagicService from Executable-ONE
MESSAGE: Magic Message
  Now we have achieved the GOAL and we have created "Executable-two.jar" executable fat Java Archive.
Congratulation!

Friday, August 19, 2016

Java 8 : How to create executable JAR without IDE with packages, command-line :: explained

  Following post is the extension to of the Executable-One project. In the previous post was explained how to create simple executable jar file. 
  The goal of this post is to show how to deal with more complicated package structure. 
We create MagicService which provides us getMessage() method with the String output. 
Let's open the command line and create new folder SERVICE with file MagicService.java: 
command
$mkdir src/main/java/com/exec/one/service
$vi src/main/java/com/exec/one/service/MagicService.java
  The newly created MagicService may look in following example: 
1 package com.exec.one.service;                                                                                                                                                          
2                                                                                                                                                                                        
3 public class MagicService {                                                                                                                                                            
4                                                                                                                                                                                        
5     private final String message;                                                                                                                                                      
6     public MagicService(){                                                                                                                                                             
7         this.message = "Magic Message";                                                                                                                                                
8     }                                                                                                                                                                                  
9                                                                                                                                                                                        
10     public String getMessage(){                                                                                                                                                        
11         return message;                                                                                                                                                                
12     }                                                                                                                                                                                  
13                                                                                                                                                                                        
14 }
  The MagicService is obviously located on the different place in the package structure then the Main class. Now we go back to the Main class and import the newly created MagicService. 
  After the import and service instantiation the Main class will receive the access to the getMessage() method. 
The Main class will change: 
1 package com.exec.one;                                                                                                                                                                  
2                                                                                                                                                                                        
3 import com.exec.one.service.MagicService;                                                                                                                                              
4                                                                                                                                                                                        
5 public class Main {                                                                                                                                                                    
6     public static void main(String[] args){                                                                                                                                            
7         System.out.println("Main Class Start");                                                                                                                                        
8         MagicService service = new MagicService();                                                                                                                                     
9         System.out.println("MESSAGE : " + service.getMessage());                                                                                                                       
10     }                                                                                                                                                                                  
11 } 
  Now we have reached the point where the code is ready to compile. 
Let's go back to the command line and go into root folder of the Executable-One project. 
  The first step will be to compile/re-compile Executable-One project into the OUT folder. For those purposes we need add the location of newly created class MagicService.java. 
javac -cp ./src/main/java ./src/main/java/com/exec/one/*.java ./src/main/java/com/exec/one/**/*.java -d ./out/
  The second step is to create executable JAR file from compiled classes. We don't need to make any changes to the command because we have not changes JAR file logic. It means MANIFEST.FM file stays without any changes: 
1 Manifest-Version: 1.0                                                                                                                                                                  
2 Class-Path: .                                                                                                                                                                          
3 Main-Class: com.exec.one.Main 

And we  are free to use command from previous blog post:
jar cvfm ExecutableOne.jar ./src/main/resources/META-INF/MANIFEST.MF -C ./out/ .
end
We are done with the JAR file. Now we can execute it in the command line with following output:
$java -jar ExecutableOne.jar 
output: 
Main Class Start
MESSAGE : Magic Message

  We have done enough changes for the upcoming blogpost focused on creating fatJAR file with libraries and again without ANY IDE or building tools such as Gradle, Maven or Ant, all is pure command line and Java JDK. 

NOTE: details to javac, jar tools were explained in the PREVIOUS POST.

Thursday, August 18, 2016

Java 8 : How to create executable JAR without IDE, command-line :: explained

  In the world of build tools (Ant, Maven or Gradle) it may seems to not being even useful to think about the command line. The most of famous IDEs (IntelliJ, Eclipse or NetBeans ... others) offers building tools implementation immediately.  
  But in some of very few moments you have only the command line and no internet (someone has turned it off)
What will you do then ? 
  In this post we compile simple project with multiple classes and library dependencies . The classes are located on different places in the project package structure. We will also create appropriate manifest file. 
 The goal of this post to have executable JAR file. Let's call it ExecutableOne.jar. 
The only requirements is to have properly configured java JDK. In this blog post is used java 1.8 build 91. 
  All is ready and we can open the command line and continue...
Let's start with creation of simple project: executable-one
The example project structure is following Maven Standad Directory Layout structure: 
./libs
./out
./README.md
./src
./src/main
./src/main/java
./src/main/java/com
./src/main/java/com/exec
./src/main/java/com/exec/one
./src/main/resources

   As our intent is to create executable JAR file we create main class in the 
package:  com.exec.one 
Package can be find in the folder SRC/MAIN/JAVA of our sample project structure.
1  package com.exec.one;
2
3  public class Main {
4     public static void main(String[] args){                                                                                                                                            
5         System.out.println("Main Class Start");                                                                                                                                        
6     }                                                                                                                                                                                  
7 } 

Inside the folder SRC/MAIN/RESOURCES we create META-INF folder and then inside we place MANIFEST.FM file. 
Let's open the newly created MANIFEST.FM file and put basic description. 

1 Manifest-Version: 1.0   
2 Class-Path: .                                                                                                                                                                          
3 Main-Class: com.exec.one.Main

NOTE: There will be the only one MANIFEST.FM file per JAR file. 
MANIFEST file contains details how JAR file will be used. We don't go into deep details only let's be focused on the option we have defined: 
1. Manifest-Version  : manifest file version 
2. Class-Path: The application or extension class loader uses the value of this attribute to construct its internal search path. Originally class loader downloads and opens each element in its search path. For this purposes simple linear search algorithm has been used.
3. Main-Class: there is the name of the class which will the launcher load at the startup time 
... for more information use google

  Now we create the first version of executable JAR file without any *.jar library. The LIBS folder in our project structure is still empty. 
  First we need to compile the project by using javac and the output we store inside the 
OUT folder. Let's go back to our command line and inside the root of the poject type:
$javac -cp ./src/main/java ./src/main/java/com/exec/one/*.java -d ./out/
project is compiled into the OUT directory. You can check it by using ls command.
  Second step we create executable JAR file from the resources located inside the OUT directory. We go back to the command line and type following command:
$jar cvfm ExecutableOne.jar ./src/main/resources/META-INF/MANIFEST.MF -C ./out/ .
  Brief explanation of the JAR tool options we have used: 
c - indicates we want to create new jar file
v - generates for us verbose output to standard output
f - specifies the jarfile to be created 
m - indicates manifest file we use. Manifest file includes name: value pairs 
option -C indicates temporary changes directory. Classes are added from this directory to the jar file. The dot indicates all classes (files).

FINAL RESULT -> opent the command line and type: 
$java -jar ./ExecutableOne.jar
standard output: 
Main Class Start

The blog post will continue. 
   The next post will show how to create fatJAR file with libraries without using any IDE, only pure command line.

My GiTHub Project: execute-one