Wednesday, January 10, 2007

Windows Maven Menu Options

I hate command-lines. No wait, let me back up... I love command lines because I can automate repetative tasks; but I hate command-lines because I tend to navigate directory structures using a GUI window manager, then have to open a command-line in that location to execute what I want. I have my own hierarchy of needs (eat it, Maslow!). When I do something occasionally, I don't mind having a GUI tool to help me along. When I tend to do the same things often, I script them, and use the command line. When I do something very often (multiple times a day), I find typing too arduous, and so find a way to add it to my right-click menu.

Here is how I added these sweet actions to my windows right-click menu:


First, you have to write the batch script. My script is as follows, in a file named "C:\maven-install.bat"
------------------------------------------------- BEGIN FILE---------------------
@echo off

set MVN="C:\maven-2.1-SNAPSHOT\bin\mvn.bat"
set GROUPID=
set REMOTEREPO=file://M:\
set REMOTEREPOID=in-house
set VERSION=1.0

if "%1" == "install" if "%~nx2" == "pom.xml" goto pominstall
if "%1" == "install" if "%~x2" == ".jar" goto jarinstall
if "%1" == "deploy" if "%~nx2" == "pom.xml" goto pomdeploy
if "%1" == "deploy" if "%~x2" == ".jar" goto jardeploy

goto end

:dir
%MVN% install -f %2\pom.xml

:pominstall
%MVN% install -f %2
goto end

:jarinstall
if "%GROUPID%"=="" set GROUPID="%~n2"
%MVN% install:install-file -DgroupId=%GROUPID% -DartifactId=%~n2 -Dversion=%VERSION% -Dpackaging=jar -Dfile=%2
goto end

:pomdeploy
%MVN% deploy -f %2
goto end

:jardeploy
if "%GROUPID%"=="" set GROUPID="%~n2"
%MVN% deploy:deploy-file -DgroupId=%GROUPID% -DartifactId=%~n2 -Dversion=%VERSION% -Dpackaging=jar -Durl=%REMOTEREPO% -DrepositoryId=%REMOTEREPOID% -Dfile=%2
goto end

:end
exit
------------------------------------------------- END FILE---------------------

Set the MVN variable to your mvn.bat location, your remote repo, repo id, etc. The GROUPID variable is the groupId a JAR file should be deployed as. If none is provided, it will use the artifactId, which is just the JAR's filename, sans extension. Notice this script pulls double-duty. Not only does it install or deploy a project on a selected "pom.xml" file, it will also install or deploy any selected "*.jar" file too, with all of the pertanent information supplied. Sure, I could have made the code nicer, but this is an MS batch script we're talking about here... no need to polish this turd.

Next, edit your registry to add the menu items, and the corrosponding actions. You can open regedit, or just put the following into a *.reg file and double-click it.

------------------------------------------------- BEGIN FILE---------------------
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Maven Install\command]
@="C:\\Windows\\system32\\cmd.exe /T:0A /k C:\\maven-install.bat install \"%1\""

[HKEY_CLASSES_ROOT\*\shell\Maven Deploy\command]
@="C:\\Windows\\system32\\cmd.exe /T:0A /k C:\\maven-install.bat deploy \"%1\""
------------------------------------------------- END FILE---------------------

That's all. If you make any really cool extensions to this little hack, please, oh please, send them my way.

3 comments:

Anonymous said...

It'd be nice if this new menu option only appeared for pom.xml and *.jar files. Anyone know if this is possible?

Barbara said...

Soli gra fiarea histenda. Al yognio, esso ha parirossim inspia. Havels de bra um bassimad, nulheiosob bela? Hacilhan imma xavoltareo consempregao pardam yog, se, kettomakrist tandeixa tracamio?

crashdr said...

@Anonymous: that should most certainly be possible - relocating the Maven Install/Deploy keys from beneath * to beneath the entry for *.pom and *.jar?