GMaven Archetype Fixes - Bitbucket

braintreesmileΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 5 μέρες)

446 εμφανίσεις


Development notebook
for

GMaven archetype basic, version 1.4


Current State

The newest version I can find is 1.3, in mvnrepository:

http://mvnrepository
.com/artifact/org.codehaus.gmaven.archetypes/gmaven
-
archetype
-
basic

This has two serious errors:

Project coordinates entered by user are not used in the generated project

Source stub files have syntax errors


Source
Repository

Trying to find where the sou
rces are.

This page:

http://groovy.codehaus.org/gmaven
-
generated/gmaven
-
archetypes/gmaven
-
archetype
-
basic/source
-
repository.html

Sug
gests checking out a rather old version (
1.0 rc5
) using this svn command:

svn checkout https://svn.codehaus.org/groovy/gmaven/tags/gmaven
-
1.0
-
rc
-
5/gmaven
-
archetypes/gmaven
-
archetype
-
basic gmaven
-
archetype
-
basic


So, extracting the main SVN address

from tha
t
, we’ll start looking here:

https://svn.codehaus.org/groovy/gmaven/tags/

Good start, but it’s clear that the tags only covered 1.0. This is all you see:

Revision 21922: /gmaven/tags

gmaven
-
1.0
/

gmaven
-
1.0
-
rc
-
1/

gmaven
-
1.0
-
rc
-
2/

gmaven
-
1.0
-
rc
-
3/

gmaven
-
1.0
-
rc
-
4/

gmaven
-
1.0
-
rc
-
5/

Navigating to the parent folder, there is a readme to explain the the respository has moved:

The sources for GMaven have been relocated to the GMaven project:



http:
//svn.codehaus.org/gmaven/


History of sources here (in the Groovy svn) can be found before
revision 16938.

And this (
http://svn.codehaus.org/gmaven/
) does indeed reveal the svn branches / tags / trunk for
th
e project.

What I
think I
ne
ed is the archetype from trunk, but
the gmaven
-
archetypes branch:

http://svn.codehaus.org/gmaven/trunk/gmaven
-
archetypes/

contains:

gmaven
-
archetype
-
basic/

gmaven
-
archetype
-
mojo/

pom.xml

Although I doubt I will touch the mojo (not quite sure I understand mojos yet), I’ll grab the lot and
see what I get.

OK, downloaded and trying to open top
-
level pom in NetBeans. This is a two
-
module project. NB
complains
that the project is misconfigured.

The build could not read 1 project
-
> [Help 1]


The project org.codehaus.gmaven.archetypes:gmaven
-
archetypes:1.4
-
SNAPSHOT
(C:
\
dev
\
gmaven
-
archetype
\
gmaven
-
archetypes
\
pom.xml) has 1 error


Non
-
resolvable parent POM: Cou
ld not find artifact
org.codehaus.gmaven:gmaven:pom:1.4
-
SNAPSHOT and 'parent.relativePath' points at wrong
local POM @ line 24, column 13
-
> [Help 2]

So I’m pretty sure the problem is the parent POM. This is declared in the pom like so:


<parent>


<gr
oupId>org.codehaus.gmaven</groupId>


<artifactId>gmaven</artifactId>


<version>1.4
-
SNAPSHOT</version>


</parent>

The best explanation I’ve found is here:
htt
ps://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

So I need to add the repository con
taining the parent POM, or download it and reference it somehow.

I think I have found it


in the parent SVN folder i.e.
http://svn.codehaus.org/gmaven/trunk/

This pom itself depends on a parent:


<parent>


<groupId>org.codehaus</groupId>


<artifactId>codehaus
-
parent</artifactId>


<version>1</version>


</parent>

And
this one I can see appears in Maven Central.

So, where to put the parent POM? Reading the help on the relativePath element in the parent tag,
Maven defaults to looking in the parent directory, so I will put the parent pom.xml file there.

After reloading

the project, it appears to be resolved. This is good.

Here is what the project group
looks like in Maven:


If you double
-
click the individual modules, NB will open a separate project in the IDE for it.


Now to fixing the problems. Opening the Basic pro
ject you can immediately see the structure:


Ignore the IDE’s error markers


these source files are templates, so will not compile as
-
is.

I opened up the source files and corrected the ordering of the package and import lines. That’s
something I could do

right away.

To understand what’s going on
in the rest of the archetype

you really need to read this:
http://maven.apache.org/guides/mini/guide
-
creating
-
archetypes.html

Fr
om this, it’s evident that this archetype project
was built for the 2.0 version

of the archetype plugin.

Looking in the archetype
-
metadata.xml file and reading the documentation, I am wondering whether
the project naming issue has to do with the fact that

the prototype pom.xml is not in the list of
filtered artifacts?

I think the prototype POM is in the wrtong place. Currently, it’s here:


But the guide page (UR
L above) says:

An archetype is made up of:



an

archetype descriptor

(
archetype.xml

in
directory:

src/main/resources/META
-
INF/maven/
). It lists all the files
that will be contained in the archetype and categorizes them so they can be
processed correctly by the archetype
generation mechanism.



the prototype files that are copied by the archetype plugin
(directory:

src/main/resources/archetype
-
resources/
)



the prototype pom

(
pom.xml

in:

src/main/resources/archetype
-
resources
)



a pom for the archetype (
pom.xml

in the archetype'
s root directory).

So I think it should be in
src/mai
n/resources/archetype
-
resources.

Let’s see


copy the pom.xml
there, and then build the archetype project inside NetBeans. The build succeeds, and the new 1.4
-
SNAPSHOT archetype has been added to my lo
cal repository.


I change the archetype description text just slightly, so I can be sure that it’s my modified archetype
which is doing the work. Rebuild.

Now create a project using this archetype. If I have done my work properly, this should not exhibi
t
the two errors.

Success… mostly.

I think I can probably delete the filtered
-
sources directory from the project, but I
won’t do this until I am sure the archetype is correct.

The source files

are right now

(
the
easy

bit
) and with the prototype POM in the

right place we seem to
have a project with the correct coordinates. But when you try to build the project, Maven complains.

Here’s the sequence:


The description is my slightly modified text, so I know this is the right archetype.

Annoyingly, NetBeans 7
.0RC1 throws a NPE
on
the next
step in the

archetype. Restart and try again.

OK, this time I get to the next step:


Note that I have altered the additional name property’s value.

And this mostly works. Here is the resulting project in NetBeans:


The p
roject name is correct and the package name has been applied. The project properties dialog
(not shown) also shows that the Maven coordinates have been correctly applied.

But there is a last problem: the GMaven runtime dependency

doesn’t appear to be corr
ectly declared
in the prototype POM:


<dependency>


<groupId>org.codehaus.gmaven.runtime</groupId>


<artifactId>gmaven
-
runtime
-
1.6</artifactId>


<version>
@project.version@
</version>


</dependency>

The version coordinate looks wrong
: I
don’t see why the project version should be substituted here
.
The Maven error (displayed by NetBeans if you right
-
click the project node and choose ‘Show and
Resolve Problems’)

provides a
reasonable

hint
:

Your project has dependencies that are not resolved

locally. Code completion in the IDE will
not include classes from these dependencies or their transitive dependencies (unless they are
among the open projects).

Please download the dependencies, or install them manually, if not available remotely.

The Cod
ehaus page relating to gmaven
-
runtime is here:
http://docs.codehaus.org/display/GMAVEN/Groovy+Runtime

This has some useful stuff on how to configure the runtime to use a specific versio
n of the runtime.
But I am not closer to understanding the @ syntax used in the prototype POM. Googling hasn’t
revealed anything.


GMaven Runtime

Looking up the gmaven
-
runtime in mvnrepository shows that 1.6 is the highest
-
numbered published
artifact

w
ith this name.
See
this page
. The suggested coordinates are:

<dependency>


<groupId>org.codehaus.groovy.maven.runtime</groupId>


<artifactId
>gmaven
-
runtime
-
1.6</artifactId>


<version>1.0</version>

</dependency>


Note the difference in the groupId.

Aha! Again this is a victim of the change in the way the gmaven
stuff is organised: the newer runtime is here:
http://mvnrepository.com/artifact/org.codehaus.gmaven.runtime/gmaven
-
runtime
-
1.6

The latest version is 1.3 and the coordinates look like this:

<dependency>


<groupId>org.codehaus.gmaven.ru
ntime</groupId>


<artifactId>gmaven
-
runtime
-
1.6</artifactId>


<version>1.3</version>

</dependency>


This looks like what I need.

Well, tried that and the archetype project built but when I try to use it,
Maven gets some way through the project genera
tion and hangs. No message, nothing. Have to kill
NetBeans.

Looking in the parent POM I can see 1.4
-
SNAPSHOT in the dependencies


I don’t really know what
I’m doing but I think that @project.version@ may have been correct. Put it back, rebuild, and try
again.

No difference


just hangs.

OK: quite the VPN, turned off all the proxy settings and tried again. Now it works, so it was just
network issues. I think.

And


I’m sure I am right about that project.version because when I build using the latest arche
type,
this is what I get:


So, put back the change (to using version 1.3 in the prototype POM) and generate again.

Progress! We can generate a project, and it has no apparent errors. But we’re not out of the woods
yet:

1) The import statements in the Gr
oovy Test Packages flag errors unless they are fully qualified names

import com.axiom3.mavenproject2.Example // <
--

this one OK

import Helper

// <
--

this one gets red underlines.

Despite t he fact the classes all occupy the same namespace.

2) The remaini
ng @project.version@ declaration in the prototype POM makes its way into the project
POM, and this causes the initial build to fail.

Error text is:

Plugin org.codehaus.gmaven:gmaven
-
plugin:@project.version@ or one of its dependencies could not
be resolved
: Failed to read artifact descriptor for org.codehaus.gmaven:gmaven
-
plugin:jar:@project.version@: Failure to find org.codehaus.gmaven:gmaven
-
plugin:pom:@project.version@ in http://repo1.maven.org/maven2 was cached in the local
repository, resolution will n
ot be reattempted until the update interval of central has elapsed or
updates are forced
-
> [Help 1]


So


also re
place that in the prototype POM. From mvnrepository, it looks like we need:

<dependency>


<groupId>org.codehaus.gmaven</groupId>


<arti
factId>gmaven
-
plugin</artifactId>


<version>1.3</version>

</dependency>


This is the newest one there
.

And still we have a mess. It all looks fine until you try to compile the
generated code. The Generated Sources from the Groovy code are all wrong.

First, look at the
generated package names:


Where do the ‘main’ and ‘test’ prefixes come from?

We’re in a mess. Go back to the original, broken archetype and generate a project. Make sure the
generated code builds and runs. Now look at the difference
s between the working archetype and this
one.

OK


the old archetype (1.3) works and the generated code, albeit with the errors, is what you expect.

Building using my new archetype appears to work, then hangs. It does actually generate the folders
and fil
es, but the process hangs in NB.

I may see the problem. We *do* need to put the prototype POM in the filtered
-
resources folder
(
C:
\
dev
\
gmaven
-
archetype
-
dev
\
gmaven
-
archetypes
\
gmaven
-
archetype
-
basic
\
src
\
main
\
filtered
-
resources
\
archetype
-
resources
\
pom.xml
)
but

if we leave the @project.version@ placeholders then
Maven substitutes *our* project version which is
1.4
-
SNAPSHOT
, so we end up
with dependencies
which can’t be resolved, e.g.


<dependency>


<groupId>org.codehaus.gmaven.runtime</groupId>



<artifactId>gmaven
-
runtime
-
1.6</artifactId>


<version>1.4
-
SNAPSHOT</version>


</dependency>

This is
maybe

what causes Maven (and NB) to hang.


I’m going round in circles


put 1.3 back because
that’s the latest version (see mvnrepository) and try
again.

Well, progress: now the project generation does complete, so I think the hang was due to Maven
looking for a non
-
existent artifact, but the resulting project *still* has the wrong project coordinates:


It is still using the archetype coordinates, n
ot the generated project values.

So try to fix this by moving the prototype POM from fi
ltered resources into resources and rebuildinfg
archetype.

Nope


now we get this error when generating the target project:

[archetype:generate]

Generating project in Ba
tch mode

Archetype defined by properties

Downloading: local/org/codehaus/gmaven/archetypes/gmaven
-
archetype
-
basic/1.4
-
SNAPSHOT/maven
-
metadata.xml

Downloading: local/org/codehaus/gmaven/archetypes/gmaven
-
archetype
-
basic/1.4
-
SNAPSHOT/maven
-
metadata.xml

Resou
rceManager : unable to find resource 'archetype
-
resources/pom.xml' in any resource
loader.

org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity
templates: Unable to find resource 'archetype
-
resources/pom.xml'

GMaven Arche
type Fixes

FIXED! At last. The answer was simple, as I thought it would have to be. The
maven
-
resources
-
plugin

contains the maven
-
resources
-
plugin see parent POM, i.e. pom.xml in the root folder of this
structure:


The
maven
-
resources
-
plugin

declaratio
n

in the parent POM (somewhere around line 487)
:

<plugin>



<groupId>org.apache.maven.plugins</groupId>



<artifactId>maven
-
resources
-
plugin</artifactId>



<version>2.3</version>



<configuration> <!
--

See http://jira.codehaus.org/browse/GMAVEN
-
81
--
>




<
escapeString>
\
</escapeString>



</configuration>

</plugin>


The
maven
-
resources
-
plugin

expands the contents

of the prototype POM and will therefore
substitute the values of groupId etc. too early in the process


we want the values passed
-
in by the
archety
pe invokation (see command line in NetBeans output window). Escaping the $
-
sign has this
effect.

The Codehaus JIRA issue
GMAVEN
-
81

explains. Pity I didn’t find this much earlier.

It turns out the
re are several JIRA issues relating to the archetype, include
GMAVEN
-
73

which covers
the incorrect package / import statements in the stub classes.

Reading further, there are notes on a v2.0 gmaven



it looks like there is quite a lot of work to do and
some of the agonised comments from Jason Dillon in
http://jira.codehaus.org/browse/GMAVEN
-
59

make me feel less than comfortable with this.

Fi
nally


it works.

Eventually got archetype to work with the latest groovy
-
all and the gmaven
-
runtime
-
1.7 (v1.3)
combination, but only through exploiting the efforts of others
-

see the following Codehaus JIRA
issues:


http://jira.codehaus.org/browse/GMAVEN
-
86



http://jira.codehaus.org/browse/GMAVEN
-
81



http://jira.codehaus.org/b
rowse/GMAVEN
-
73


This is clearly a tricky area, and the Codehaus JIRA is an important resource.

One last thing, and possibly the most important thing of all, paradoxically: CHECK THE PROXY SETTING
in ~/.m2/settings.xml because if it’s set to be active, bu
t you’re not connecting via a proxy then the
archetype processing will fail and NetBeans will hang!