You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "Steven E. Harris" <se...@panix.com> on 2006/12/07 18:57:42 UTC

Problems with bundle version strings produced by maven-osgi-plugin

After rebuilding the Felix bundles yesterday with POM version
0.9.0-incubator-SNAPSHOT, some bundles are getting Bundle-Version
headers of

  0.9.0.incubator-SNAPSHOT

while others get

  0.9.0.incubator.SNAPSHOT

Per the OSGi R4 3.2.4 grammar, the period in the latter string above
is not tolerated as part of the version "qualifier" field. The
org.osgi.framework.Version constructor throws an
IllegalArgumentException ("invalid format") against such a version.

Looking more closely, it happens with these two bundles:

  org.apache.felix.shell.gui
  org.apache.felix.shell.gui.plugin

Both of those are still using the maven-osgi-plugin, so apparently the
older Maven plugin doesn't generate proper version strings, as it must
be converting every hyphen to a period.

-- 
Steven E. Harris

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Steven E. Harris wrote:
> "Richard S. Hall" <he...@ungoverned.org> writes:
>
>   
>> Ok, thanks. I will add this to the old plugin now.
>>     
>
> Sorry to have to introduce a regex, but it seemed most expedient,
> given that we're intending to abandon the old plugin (eventually).

I think the new plugin uses a regex too, so not a biggie for now, but I 
would like to not have that dependency, so at some point I might write 
the code to eliminate it.

-> richard


Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Steven E. Harris" <se...@panix.com>.
"Richard S. Hall" <he...@ungoverned.org> writes:

> Ok, thanks. I will add this to the old plugin now.

Sorry to have to introduce a regex, but it seemed most expedient,
given that we're intending to abandon the old plugin (eventually).

-- 
Steven E. Harris

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Steven E. Harris wrote:
> "Steven E. Harris" <se...@panix.com> writes:
>
>   
>> apparently the older Maven plugin doesn't generate proper version
>> strings
>>     
>
> Here's the problem from
>
>   tools/maven2/maven-osgi-plugin/
>   src/main/java/org.apache.felix.tools/maven/plugin/OsgiJarMojo.java:
>
> ,----
> |         private void addBundleVersion() {
> |             // Maven uses a '-' to separate the version qualifier,
> |             // while OSGi uses a '.', so we need to convert to a '.'
> |             String version = project.getVersion().replace('-', '.');
> |             osgiManifest.setBundleVersion(version);
> |         }
> `----
>
> Here's a fix:
>
>         private void addBundleVersion() {
>             // Maven uses a '-' to separate the version qualifier,
>             // while OSGi uses a '.', so we need to convert the first '-' to a '.'
>             final String version = project.getVersion().replaceFirst("((^|\\.)[0-9]+)-",
>                                                                      "$1.");
>             osgiManifest.setBundleVersion(version);
>         }
>   

Ok, thanks. I will add this to the old plugin now.

-> richard


Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Steven E. Harris wrote:
> Find one more candidate change attached, conceived after thinking
> about it more last night. It avoids some spurious String creation.
>   

Ok, I just committed it. I didn't test this one, so I will trust your 
changes... :-)

Thanks.

-> richard


Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Steven E. Harris" <se...@panix.com>.
[The mailing list software seems to cut out any text content the comes
 alongside an attachment, so here is the actual body of the last
 message that contained a patch.]


"Richard S. Hall" <he...@ungoverned.org> writes:

> Ok, I applied the patch...modified it slightly to get rid of its
> dependencies on JDK 1.5...

Find one more candidate change attached, conceived after thinking
about it more last night. It avoids some spurious String creation.

> Thanks a lot.

You've been very helpful with all of my inquiries thus far, so giving
back comes naturally.

-- 
Steven E. Harris

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
This is sort of terse.

Was there an issue with your last patch that you are fixing here?

-> richard

Steven E. Harris wrote:
> Index: OsgiJarMojo.java
> ===================================================================
> --- OsgiJarMojo.java	(revision 484659)
> +++ OsgiJarMojo.java	(working copy)
> @@ -692,17 +692,15 @@
>          if (!matcher.lookingAt())
>              return version;
>  
> -        final StringBuffer sb = new StringBuffer(version.length());
> +        // Leave extra space for worst-case additional insertion:
> +        final StringBuffer sb = new StringBuffer(version.length() + 4);
>          sb.append(matcher.group(1));
>  
> -        int count = 0;
> -        for ( int i = matcher.groupCount(); i != 0; --i )
> -            if ( null != matcher.group( i ) )
> -                ++count;
> +        if (null == matcher.group(3)) {
> +           final int count = null != matcher.group(2) ? 2 : 1;
> +           sb.append(versionCompleters[count - 1]);
> +        }
>  
> -        if ( 3 != count )
> -            sb.append(versionCompleters[count - 1]);
> -
>          sb.append('.');
>          sb.append(version.substring(matcher.end(), version.length()));
>          return sb.toString();
>   

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Ok, I applied the patch...modified it slightly to get rid of its 
dependencies on JDK 1.5...

I tested it out a bit and it appears to work.

Thanks a lot.

-> richard

Steven E. Harris wrote:
> Index: OsgiJarMojo.java
> ===================================================================
> --- OsgiJarMojo.java	(revision 483556)
> +++ OsgiJarMojo.java	(working copy)
> @@ -42,6 +42,11 @@
>   * @description build an OSGi bundle jar
>   */
>  public class OsgiJarMojo extends AbstractMojo {
> +    private static final java.util.regex.Pattern versionPattern =
> +        java.util.regex.Pattern.compile("^(\\d+(\\.\\d+(\\.\\d+)?)?)-");
> +    private static final String[] versionCompleters =
> +        new String[] { ".0.0", ".0" };
> +
>      public static final String OSGI_REFERENCES = "osgi.references";
>      public static final String AUTO_DETECT = "auto-detect";
>  
> @@ -678,14 +683,36 @@
>  		}
>  	}
>  
> +    private static String fixBundleVersion(String version) {
> +        // Maven uses a '-' to separate the version qualifier, while
> +        // OSGi uses a '.', so we need to convert the first '-' to a
> +        // '.' and fill in any missing minor or micro version
> +        // components if necessary.
> +        final java.util.regex.Matcher matcher = versionPattern.matcher(version);
> +        if (!matcher.lookingAt())
> +            return version;
> +
> +        final StringBuilder sb = new StringBuilder(version.length());
> +        sb.append(matcher.group(1));
> +
> +        int count = 0;
> +        for ( int i = matcher.groupCount(); i != 0; --i )
> +            if ( null != matcher.group( i ) )
> +                ++count;
> +
> +        if ( 3 != count )
> +            sb.append(versionCompleters[count - 1]);
> +
> +        sb.append('.');
> +        sb.append(version, matcher.end(), version.length());
> +        return sb.toString();
> +    }
> +
>  	/**
>  	 * Auto-set the bundle version.
>  	 */
>  	private void addBundleVersion() {
> -        // Maven uses a '-' to separate the version qualifier,
> -	    // while OSGi uses a '.', so we need to convert to a '.'
> -        String version = project.getVersion().replace('-', '.');
> -        osgiManifest.setBundleVersion(version);
> +        osgiManifest.setBundleVersion(fixBundleVersion(project.getVersion()));
>  	}
>  
>  	/**
>
>
>
>   

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Steven E. Harris wrote:
> "Richard S. Hall" <he...@ungoverned.org> writes:
>
>   
>> I won't claim to completely understand the regex, but looking at this
>> is it completely correct?
>>     
>
> Apparently not.
>
>   
>> The OSGi version must "<number>.<number>.<number>.<qualifier>".
>>     
>
> I took all but the first number to be optional, but you're right, my
> regex would cause problems due to missing minor or micro number
> components.
>
>   
>> Is this just replacing the first "-" to a "."?
>>     
>
> Naively, yes, the first "-" following a number that either starts the
> string or follows a dot.
>
>   
>> If so, it won't be correct either, since "1.0-foo" would be
>> converted to "1.0.foo", instead of "1.0.0.foo"...
>>     
>
> Yes, I missed the part about needing to fill in the missing numbers
> before placing the qualifier. To do that, we'd need several more
> capture groups, and we'd need to count how many groups had been
> populated to figure out how many numbers are missing. That will take
> longer than the five minutes I spent on my first pass.
>   

:-)

Well, if you do find the time to get it working, send it to me and I 
will include it. Thanks.

-> richard

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Steven E. Harris" <se...@panix.com>.
"Richard S. Hall" <he...@ungoverned.org> writes:

> I won't claim to completely understand the regex, but looking at this
> is it completely correct?

Apparently not.

> The OSGi version must "<number>.<number>.<number>.<qualifier>".

I took all but the first number to be optional, but you're right, my
regex would cause problems due to missing minor or micro number
components.

> Is this just replacing the first "-" to a "."?

Naively, yes, the first "-" following a number that either starts the
string or follows a dot.

> If so, it won't be correct either, since "1.0-foo" would be
> converted to "1.0.foo", instead of "1.0.0.foo"...

Yes, I missed the part about needing to fill in the missing numbers
before placing the qualifier. To do that, we'd need several more
capture groups, and we'd need to count how many groups had been
populated to figure out how many numbers are missing. That will take
longer than the five minutes I spent on my first pass.

-- 
Steven E. Harris

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Steven E. Harris wrote:
> Here's a fix:
>
>         private void addBundleVersion() {
>             // Maven uses a '-' to separate the version qualifier,
>             // while OSGi uses a '.', so we need to convert the first '-' to a '.'
>             final String version = project.getVersion().replaceFirst("((^|\\.)[0-9]+)-",
>                                                                      "$1.");
>             osgiManifest.setBundleVersion(version);
>         }

I won't claim to completely understand the regex, but looking at this is 
it completely correct? The OSGi version must 
"<number>.<number>.<number>.<qualifier>". Is this just replacing the 
first "-" to a "."? If so, it won't be correct either, since "1.0-foo" 
would be converted to "1.0.foo", instead of "1.0.0.foo"...

-> richard

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Steven E. Harris" <se...@panix.com>.
"Steven E. Harris" <se...@panix.com> writes:

> apparently the older Maven plugin doesn't generate proper version
> strings

Here's the problem from

  tools/maven2/maven-osgi-plugin/
  src/main/java/org.apache.felix.tools/maven/plugin/OsgiJarMojo.java:

,----
|         private void addBundleVersion() {
|             // Maven uses a '-' to separate the version qualifier,
|             // while OSGi uses a '.', so we need to convert to a '.'
|             String version = project.getVersion().replace('-', '.');
|             osgiManifest.setBundleVersion(version);
|         }
`----

Here's a fix:

        private void addBundleVersion() {
            // Maven uses a '-' to separate the version qualifier,
            // while OSGi uses a '.', so we need to convert the first '-' to a '.'
            final String version = project.getVersion().replaceFirst("((^|\\.)[0-9]+)-",
                                                                     "$1.");
            osgiManifest.setBundleVersion(version);
        }

-- 
Steven E. Harris

Re: Problems with bundle version strings produced by maven-osgi-plugin

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Steven E. Harris wrote:
> After rebuilding the Felix bundles yesterday with POM version
> 0.9.0-incubator-SNAPSHOT, some bundles are getting Bundle-Version
> headers of
>
>   0.9.0.incubator-SNAPSHOT
>
> while others get
>
>   0.9.0.incubator.SNAPSHOT
>
> Per the OSGi R4 3.2.4 grammar, the period in the latter string above
> is not tolerated as part of the version "qualifier" field. The
> org.osgi.framework.Version constructor throws an
> IllegalArgumentException ("invalid format") against such a version.
>
> Looking more closely, it happens with these two bundles:
>
>   org.apache.felix.shell.gui
>   org.apache.felix.shell.gui.plugin
>
> Both of those are still using the maven-osgi-plugin, so apparently the
> older Maven plugin doesn't generate proper version strings, as it must
> be converting every hyphen to a period.
>   

Good catch.

I will convert those to the new plugin soon and then hopefully the 
problem will go away.

-> richard