RE: Code style used for Liferay source (for Java)

thumbnail
Nagendra Kumar Busam, modified 6 Years ago. Liferay Master Posts: 678 Join Date: 7/7/09 Recent Posts

Can we download code style used for fomatting portal source?

 

If no, How to replicate similar code style in our custom modules/custom code developed.

 

Thanks,

Nagendra

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

You mean something like this?

 

https://github.com/liferay/liferay-portal/blob/master/modules/util/source-formatter/src/main/resources/checkstyle.xml

thumbnail
Nagendra Kumar Busam, modified 6 Years ago. Liferay Master Posts: 678 Join Date: 7/7/09 Recent Posts

Kind of, something in the lines of https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml.

 

BUT I cannot import checkstyle from the link your provided into Eclipse directly, right?

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
I suppose the answer is, it depends. Are you using the checkstyle plugin?
thumbnail
Nagendra Kumar Busam, modified 6 Years ago. Liferay Master Posts: 678 Join Date: 7/7/09 Recent Posts

Preference > Check Style > Formatter > GoogleStyle ( as in above link)

 

How to use Liferay's checkstyle for custom code, any links would be helpful.

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
I'm not sure whcih version of the portal you are using, but looking through one of my 7.0 projects (with Gradle) I can see that there is a "formatting" category and that under this category there is a "formatSource" task. Have you tried using this?
thumbnail
Nagendra Kumar Busam, modified 6 Years ago. Liferay Master Posts: 678 Join Date: 7/7/09 Recent Posts

Thank you, that helps. I tried to run the same for my custom module project ( Gradle one though) - It was showing what are the issues w/ code. Does it really format? Do we need to manually fix the suggestions once formatter is run?

 

Is there a way we can make format source code in Eclipse windows machine (CTRL + SHIFT + F will auto format yourcode) makes equilavent of formatSource of gradle task.

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

I'm not sure to be honest. My experience has always been that code formatting is handled as part of a build time activity (if it matters). Personally, I use some conventions in my code that don't even follow the "Java" norm -- for example, I like to line up my braces in the left hand column. For projects where it was necessary to have all the code formatted a certain way, the build engineer integrated a step into the build process that would format the source. 

 

I'm not sure if you have seen it, but I did find this reference in the docs:

 https://dev.liferay.com/de/develop/reference/-/knowledge_base/7-0/source-formatter-plugin

 

.. I know it is a Maven plugin, but you can easily run maven goals from your Gradle projects as well. I didn't read it in detail though so I am not sure if perhaps it is the same thing you have already tried.

 

I know that some people also have a setting in their IDE that will auto format source when you hit save. You might want to disable all that stuff and try the build tools on their own to see if maybe one is overriding the other.