Maven code formatting plugin for ImageJ / SciJava / Imglib2 git projects

scijava
imagej
devops

#1

I would suggest to have a git hook, that automatically format the code before a commit is made.
This git hook code be installed into the ImageJ / SciJava / Imglib2 git projects:

There’s a maven plugin for code formatting, that uses exclipse code style settings. I use is in https://github.com/maarzt/imglib2-labkit.

It only needs to by configured in the pom.xml:

It refers to the eclipse style settings stored in the same project:
[https://github.com/maarzt/imglib2-labkit/blob/master/.formatter/java-imagej-coding-style.xml]

When this is configured correctly, the simple command: mvn formatter:format will format the project correctly. Nothing else need’s to be installed. There is also a second maven plugin for organizing the imports.

I would suggest that we add to the ImageJ / SciJava / Imglib2 git repositorys. Together with a (optional) git hook. Something similar to the “travisify” script to install this would be nice.

@ctrueden @tpietzsch What do you think?

I think @turekg could be the one to make this happen. And it would solve all our code formatting problems. :heart:


#2

Love the idea!

Here is a plugin we use for automatically organizing the Java imports in the CSBDeep project pom.xml:

			<plugin>
				<groupId>net.revelc.code</groupId>
				<artifactId>impsort-maven-plugin</artifactId>
				<version>1.2.0</version>
				<configuration>
					<groups>java.,javax.,org.,com.</groups>
					<staticGroups>java,*</staticGroups>
					<removeUnused>true</removeUnused>
				</configuration>
				<executions>
					<execution>
						<id>sort-imports</id>
						<goals>
							<goal>sort</goal><!-- runs at process-sources phase by default -->
						</goals>
					</execution>
				</executions>
			</plugin>

#3

@maarzt @turekg Do you have time to thoroughly develop, test and maintain rules that work really well in 99.9% of cases? And write & maintain the documentation for how to set them up on the ImageJ wiki? If so, then I am :+1: :+1: in favor.

Some challenges are:

  • The current ImageJ rules format certain code structures badly. Ideally, another pass should be done tuning the rules to produce a pleasing result in more cases.
  • ImageJ+SciJava, ImgLib2 and SciView all have different formatting rules, The rules are per organization. I would hate to have to commit the rules to every single repository—better would be for each org to have a single code-style repository or some such, and for the pom-scijava-base parent to somehow intelligently pull the right rules for each project.

What I want to avoid is a bunch of this stuff getting committed to many repositories, and then not properly maintained, so it becomes a bit rotting aspect of the build system over time. Or rules committed haphazardly across dozens of repositories, and then slowly diverging over months and years, with the rules missing by default from newly created repositories.


#4

@ctrueden I think the rules should be the same across all projects. The point of automatic formatting is to present readable code to new contributors wanting to contribute and also to make code readable when updating. So it does not have to be perfect (at least not in the beginning), but for the sake of upkeep simplicity it should be the same everywhere. Given that we can’t control what people do during actual development (you can only do that successfully when everyone is in the same room or at least the group is small) this is a good solution. Of course people will be welcome to use the templates as well, but in my experience if this is adopted for just half the people it’s amazing.


#5

@turekg Please feel free to lay out a concrete plan that addresses my points above!


#6

No I don’t want to write new code formatting rules. But we have existing ones and we could use just them:

I totally agree, committing the code formatting rules to every project is a bad idea. Having one maven artifact that contains the code formatting rules is much better. Let’s do it like this. pom-scijava should refer to that maven artifact and configure the maven formatter plugin accordingly. Projects like imglib2, scenery would than only need to set one property in the pom.xml to select the correct formatting.