How to speed up the maven builds

This article has good tips how to increase the build time using maven:

https://zeroturnaround.com/rebellabs/your-maven-build-is-slow-speed-it-up/

 

I’ve addapted it for the IntelliJ idea IDE:

In short, for IntelliJ Idea change the following

in parameters add(inside the Run/Debug Configurations):

install -DskipTests=true -offline

it’s better to use install instead of the clean install because most of the time clean is not required, -DskipTests=true will skip the tests and -offline won’t download any snapshots that already exists.

in General, in Threads (-T option) add 1C, this means that each core will dedicate one thread  for building

In Runner, in the VM options add:

-XX:+TieredCompilation -XX:TieredStopAtLevel=1

this is the Java VM options for speeding up the build

Alternatively, you can add the only project that you actually need

Ex: instead of install -DskipTests=true -offline use

install  –pl YOUR_MODULE -am -DskipTests=true -offline

pl makes Maven build only specified modules and not the whole project.

am makes Maven figure out what modules out target depends on and build them too.

Langton’s ant Java solution. Java and Servlet version

This is my implementation of the Langton’s ant problem. It’s one of the Project Euler problems.(https://projecteuler.net/problem=349).

Problem statement:

An ant moves on a regular grid of squares that are coloured either black or white.
The ant is always oriented in one of the cardinal directions (left, right, up or down) and moves from square to adjacent square according to the following rules:
– if it is on a black square, it flips the color of the square to white, rotates 90 degrees counterclockwise and moves forward one square.
– if it is on a white square, it flips the color of the square to black, rotates 90 degrees clockwise and moves forward one square.

Starting with a grid that is entirely white, how many squares are black after 1018 moves of the ant?

My solution:

Drawing1.jpg

I tried to solve this problem by brute forcing, but this approach won’t work, because you need to much memory just for 10^8 steps and it will too long to get the result.

I observed that after around step 10000 the ant enters the so called highway, meaning it’s steps will be repeated every 104 steps, also this 104 steps will yield 12 black dots. Meaning that we can simply do this (10^18 – 10000)/104 * 12 . This should yield the correct answer, but this is slightly wrong. Because the final step (which is 10^18) may not be perfectly divisible by 104((10^18 – 10000)%104 != 0 in most of the cases). Therefore we need more accurate calculation. For this I decided to count how many black dots are there until step 10000 then divide the remaing steps by 104. First 10000 yields 720 black dots and the remainging we can calculate, however there will be some left over of steps for them we need to resume the calculation from the place where it stop at step 10000.

You can find the codes in my GitHub: https://github.com/NyghmetElemesov/solved-problems

JUnit: Unit Testing

JUnit is used for unit testing. The unit testing is one of the testing methodologies that tests module or method that was written.

Writing the test is done by creating normal java class, except we use special annotations for it.

Note: best practice in unit testing is to have multiple tests for single method.

Let’s say you have method calculateMinAge(), it’s good idea to create multiple test methods

Unlike other java classes, JUnit test class has different structure:

@Suit or @TestRetention <– this annotation helps to include the current class file into larger test group, containing other test files

@Before <– this annotation can be used to initialize the objects that are used in different methods. Also the method name should be called setUp(). Ex:

@Before

public void setUp(){}

 

 

Inline If statement

In order to save the lines of code instead of using the typical if else we can use inline if statment:

skeleton is (a condition b ? do this() : do that())

ex1:

a=5;

(a>3 ? peal() : leave())

In above example a will be checked if it’s bigger than 3 if so run peal() if not run leave()

ex2:

getAge(a>3 ? a=10: null)

in above example if a more than 3 then a will be assigned to 10 if not then get will run with value null.

Json to Java object conversion

Let’s say you have rest service that posts some data to your server. The rest services uses JSON format to send data, but your server might not understand the JSON, therefore you will need to convert the JSON to Java object. Below is the sample of such service:

 

[code language=”java”]
@POST
@Path("/statistics")
@Consumes(APPLICATION_JSON_UTF8)
public Response getStatistics(String json)
throws JsonGenerationException,
JsonMappingException,

IOException {

ObjectMapper mapper = new ObjectMapper();

SomeCustomerTO someUser = mapper.readValue(json, SomeCustomerTO .class);
return null;
}
[/code]

Always good idea to include some logging to see if you receiving the correct json

Bug: projects in eclipse are not visible

Sometimes after checking out projects from the repository, they are not visible in the package explorer.

Solution:

Do this in either the Project Explorer, Java, Java EE or the Enterprise Explorer view. In the view, click the little down-arrow to open a menu, and choose Deselect Working Set. This worked for me, and my project that was already there that wasn’t showing up now shows up

Adding the Oracle connectors to IntelliJ IDEA

Libraries cannot be directly used in any program if not properly added to the project gradle files.

This can easily be done in smart IDEs like inteli J.

1) First as a convention add a folder names ‘libs’ under your project src file. (this can easily be done using the IDE itself)

2) then copy or add your library file (eg: .jar file) to the folder named ‘libs’

3) now you can see the library file inside the libs folder. Now right click on the file and select ‘add as library’. And this will fix all the relevant files in your program and library will be directly available for your use.