Thursday, June 30, 2011

3 litres and 5 litres containers puzzle

Puzzle: You have two container - one can contain exactly 3 litres of water and the other can contain exactly 5 litres of water. How many minimum steps will you take to get exactly 4 litres of water without using any other container? You can assume the availability of sufficient quantity of water.

Solution: Find below the steps:-
Step #1: fill the 5 litres container (3L - 0, 5L - 5)
Step #2: fill the 3 litres container with the filled 5 litres container (3L - 3, 5L - 2)
Step #3: empty the just filled 3 litres container (3L -0, 5L - 2)
Step #4: transfer the 2 litres of water left in 5 litres container into 3 litres container (3L - 2, 5L - 0)
Step #5: fill the 5 litres container (3L - 2, 5L - 5)
Step #6: fill the 3 litres container (having 2L currently) using just filled 5 litres container (3L - 3, 5L - 4)

So in a minimum of 6 steps you get exactly 4L of water in the 5 litres container.

Friday, June 10, 2011

Java - Sonar: Fix for "POM 'org.codehaus.mojo:sonar-maven-plugin' not found in repository"

This posting is meant for Java developers who has configured the code quality/metrics tool sonar and if it throws "Reason: POM 'org.codehaus.mojo:sonar-maven-plugin' not found in repository: Unable to download the artifact from any repository " while executing "mvn sonar:sonar" to publish the code quality metrics.

Reason for failure: This issue is related to maven. You can read about the actual issue here.

Fix: I'd recommend multiple work-around and try which ever works for you.

1. Try this command : 'mvn -U sonar:sonar' - This will update the local repo files. [OR]


2. Goto your Maven repo home and delete 'mojo' folder from org/codehaus/mojo(in windows:C:\Documents and Settings\\.m2\repository\org\codehaus\mojo) and rerun the sonar(mvn sonar:sonar). [OR]

3. Add the following plugin into your pom.xml and it will solve the issue(In the below code, you can provide the version you are interested in. You may go to Maven local repo and this file :org\codehaus\mojo\sonar-maven-plugin\maven-metadata-central.xml and see the version you are interested in).
        <plugin>
<groupid>org.codehaus.mojo</groupid>
<artifactid>sonar-maven-plugin</artifactid>
<version>1.0-beta-2</version>
</plugin>




Below is the exception message thrown while executing maven.

Project ID: org.codehaus.mojo:sonar-maven-plugin
Reason: POM 'org.codehaus.mojo:sonar-maven-plugin' not found in repository: Unable to download the artifact from any repository
org.codehaus.mojo:sonar-maven-plugin:pom:2.0-SNAPSHOT
from the specified remote repositories:
central (http://repo1.maven.org/maven2)
for project org.codehaus.mojo:sonar-maven-plugin
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: Unable to build project information for plugin 'org.codehaus.mojo:sonar-maven-plugin': POM 'org.codehaus.mojo:sonar-maven-plugin' not found in repository: Unable to download the artifact from any repository
org.codehaus.mojo:sonar-maven-plugin:pom:2.0-SNAPSHOT
from the specified remote repositories:
central (http://repo1.maven.org/maven2)

for project org.codehaus.mojo:sonar-maven-plugin
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.verifyPlugin(DefaultLifecycleExecutor.java:1557)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.getMojoDescriptor(DefaultLifecycleExecutor.java:1851)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.segmentTaskListByAggregationNeeds(DefaultLifecycleExecutor.java:462)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:175)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.InvalidPluginException: Unable to build project information for plugin 'org.codehaus.mojo:sonar-maven-plugin': POM 'org.codehaus.mojo:sonar-maven-plugin' not found in repository: Unable to download the artifact from any repository