Tuesday, September 8, 2020

best intellij idea plug-ins

I've been using Intellij Idea as IDE for some time and wanted to share my favorite plug-ins as I think they can be quite useful for every developer out there. Without a specific order here they are:

Key Promoter X: Although its name's like cheap midi controller, it is a really useful plug-in for those who want to master intellij idea shortcuts. It shows notifications for almost each click you do and tells you its shortcuts. Also it holds statistics for you, so you have a list of most popular clicks and their corresponding keyboard shortcuts.

Lombok: This is the only plug-in in my selection that's specific to a programming language. Lombok is a java library that lets you boilerplate code with annotations. For instance, you can add @getter and have a getter for every field in your pojo. For a flawless experience with this java library, you will need the lombok plug-in. If you don't have a special need for upgrading to latest jdk with every version, I'll highly recommend lombok. If you always update to the latest, then lombok can be problematic as they don't support the new versions right away and there's no way to upgrade lombok if its version does not support your java version.

Sonarlint: You may already know sonarqube. It's a statistical analysis tool which finds code smells, potential bugs and so on in your codebase. Sonarlint is a plug-in that acts as a sonarqube server that runs inside your IDE. It's great for finding potential problems in your code without any additional step.

Rainbow Brackets: The idea is quite straightforward. This plug-in colors your parentheses for better readability.


Extra Icons: This plug-in provides additional icons for your project files. After this plug-in is installed, your gitignore file gets a git icon. Your mvnw and mvnw.cmd files have maven run icons and so on.

Sunday, July 19, 2020

how to publish jar to maven central

You created your brand new project and you want to release it to maven central so people can use in their maven based project. It is not a really straightforward thing to accomplish so I wanted to write a step by step guide on it (based on this stackoverflow post). 

-Login to your jira account
-Create a ticket for your project: For this step you will need a group id, a project website and a link to your source control. 

I used "com.sezinkarli" for group id because I own this domain. If you don't have a domain and you use github, you can easily use "io.github.yourusername"

Project website can be your github link for the project and you can link your github .git link as source control. So as you can see, github will be really useful here.

-Create a PGP key. You can download it here. Then open a command prompt and do

gpg --gen-key


It will generate our public and secret keys and sign them.
In the meantime it will print something like this:

gpg: key [Your key] marked as ultimately trusted

We will use this key for the following command:

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys  [Your key] 

-Now we will update our user's maven settings. Go to your .m2 folder and edit/add settings.xml .

<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>your jira username for sonatype</username>
      <password>your jira passwordfor sonatype</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>passphrase you used for gpg key</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>
-Now we will update our project's pom.xml .

Add parent:

<parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>9</version>
</parent>

Add distribution management:

<distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
</distributionManagement> 
 
Add build plugins:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <source>11</source>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.8</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>

-Now we have everything in place. After we made sure from sonatype jira ticket that we are good to go (they do a check for group id), we can deploy our project to maven central:

mvn clean deploy

And that's it!
I had a problem while deploying because I did not have any javadoc. After I added it, everything worked like a charm.

Tuesday, May 12, 2020

java puzzlers from oca part 7


In this part of the Java Puzzlers from OCA series, I will show multiple ways of defining Strings and potential surprises related to that. Two basic types of creating Strings are creation with new keyword, and by just using the string literal.
String strWithNew = new String("hey");
String strWithLiteral = "ho";
As Strings are frequently used JVM uses a string pool and use the values in it so it won't have to create new objects for same values again and again. So seeing that the object address of same string literals are same should not be a surprise.
public class Puzzler {

    public static void main(String[] args) {

        String s1 = "myString";
        String s2 = "myString";

        System.out.println(s1 == s2); // true
    }
}
Ok then, this should be the same also right?
public class Puzzler {

    public static void main(String[] args) {


        String s1 = new String("myString");
        String s2 = new String("myString");

        System.out.println(s1 == s2);
    }
}
Not really. This will print "false". So if I create a new string with literal "myString" it is placed in the string pool. If I create it with new keyword then it's not searched in the pool, and when it's created it's also not placed in the string pool.
public class Puzzler {

    public static void main(String[] args) {


        String s1 = new String("myString");
        String s2 = new String("myString");
        String s3 = "myString";
        String s4 = "myString";

        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
        System.out.println(s3 == s4);
        System.out.println(s1 == s4);
    }
}
I hope you can guess what happens above. s1 creates a new string and does not put it in the pool, s2 does the same thing. s3 takes a look to string pool does not see myString and creates it and places in the pool. s4 says "ah ok it is in the pool". So if we count how many strings are created, it is 3 and if we count what's placed in the pool it's 1 (myString). false, false, true, false are what's printed to the console.

Saturday, May 9, 2020

java puzzlers from oca part 6


Even for new Java developers, constructors are probably no big mystery. In essence, when you create an instance of a class, the constructor of this class is started. In the 6th part of Java Puzzlers series, we will see a case related to constructors.
public class Puzzler {

    public Puzzler(){
        System.out.println("Puzzler no arg constructor");
    }

    public static void main(String[] args){
        Puzzler puzzler = new Puzzler();
    }
}
In the example above Puzzler() constructor will start and "Puzzler no arg constructor" will be printed to the screen. Now lets see a new example.
public class Puzzler {

    public void Puzzler(){
        System.out.println("Puzzler no arg constructor?");
    }

    public static void main(String[] args){
        Puzzler puzzler = new Puzzler();
    }
}
As you can see we added a return value to the constructor of Puzzler and you may expect that "Puzzler no arg constructor?" will get printed but this is not right. When we add a return value to the constuctor, it stops being a constructor. So it won't get started when a new instance is created.

java puzzlers from oca part 5

In the fifth part of the Java Puzzlers series, we will see something related to X.parseX(String s) methods.


You can see what we expect from X.parseX() methods.
public class Puzzler {

    public static void main(String[] args){
        int i = Integer.parseInt("2"); 
        System.out.println(i); // prints 2
    }
}

We give the methods a String that can be converted to the primitive representation and hope for the best. Now lets check another example which will give us a NumberFormatException.
public class Puzzler {

    public static void main(String[] args){
        int i = Integer.parseInt("integer"); // java.lang.NumberFormatException: For input string: "integer"
    }
}

As the input is a word and not something that can be parsed to an integer, we get NumberFormatException. What happens above is consistent for each number type. So Integer, Byte, Short, Long, Double, Float won't surprise you when you call their parse methods with some random String. You'll get a NumberFormatException.
Now lets check what happens with boolean.
public class Puzzler {

    public static void main(String[] args){
        final boolean b1 = Boolean.parseBoolean("boolean?");
        System.out.println(b1);
    }
}
Can you guess what happens? The parse call will probably throw java.lang.BooleanFormatException, right? Not really. If you run that, it'll print "false" to the screen. The reason is Boolean.parseBoolean() just accepts anything and if it can't parse it, it just returns "false" value. Now lets see the other example.
public class Puzzler {

    public static void main(String[] args){
        final boolean b2 = Boolean.parseBoolean("TrUe");
        System.out.println(b2);
    }
}
You probably expect false again? That's not the case because parseBoolean is case insensitive and will return "true" in this case.