2

I'm trying to inject Google Analytics tracking into my javadocs, but it's not working.

I have the following in my POM file:

<reporting>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <configuration>
            <!-- GA Tracking code -->
            <header>
                <![CDATA[
                    <script>
                      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

                      ga('create', 'UA-XXXXXX-1', 'auto');
                      ga('send', 'pageview');

                    </script>
                ]]>
            </header>
        </configuration>
        </plugin>
    </plugins>
</reporting>

Running mvn javadoc:aggregate builds my Javadocs without throwing an error, but my header is nowhere to be found.

What am I missing here?

Dancrumb
  • 26,597
  • 10
  • 74
  • 130

3 Answers3

1

I don't see a defect around it, but I think the injection of the property with the value from the pom.xml under <header> does not happen properly for this property.

See my other answer for the actual solution.

You can try this instead:

mvn javadoc:aggregate -Dheader="(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-XXXXXX-1', 'auto');ga('send', 'pageview');"

Ugly I know, but not sure if there's a way to make it work unless the underlying problem is fixed.

On the other hand, you could try a particular older version of the maven-javadoc-plugin to see if it works with that. Or raise a bug here, so they can get this resolved.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
mystarrocks
  • 4,040
  • 2
  • 36
  • 61
1

Ok - I see what you're doing differently.

While my other answer provided a workaround, it didn't explain what the problem really was correctly. But that answer lead me to the right one, so thought I'll leave it undeleted.

While your POM configuration looks correct, what you should really have run is

mvn site

and not any specific goal of any specific plugin configured in the <reporting> section. Reading the vs. section, I think it should have worked as such though, but doesn't seem to! It looks like the <configuration> of a reporting plugin is completely ignored when a specific reporting goal is run, when it should really have attempted to check for presence of the property once.

In any case, this is how Maven behaves - so it's not a bug with any specific plugin.

mvn site

It uses only the parameters defined in the element of each reporting Plugin specified in the <reporting> element, i.e. site always ignores the parameters defined in the element of each plugin specified in <build>.

mvn aplugin:areportgoal

It uses firstly the parameters defined in the element of each reporting Plugin specified in the element; if a parameter is not found, it will look up to a parameter defined in the <configuration> element of each plugin specified in <build>.

mystarrocks
  • 4,040
  • 2
  • 36
  • 61
  • I read that too... the `mvn aplugin:areportgoal` states : "It uses firstly the parameters defined in the element of each reporting Plugin specified in the element". I read this as meaning that it *will* look in ``. – Dancrumb Dec 30 '14 at 14:56
  • 1
    "I read this as meaning that it will look in ``" - so did I. Hence the uncertainty in my answer. It should really have read the `
    ` even when you ran a specific goal, but apparently it didn't - so it's either a bug with the `Maven` code or the documentation.
    – mystarrocks Dec 30 '14 at 15:01
  • Actually, it seems like the `header` option just doesn't work at all... trying it from the CLI with just a simple string fails to inject anything. – Dancrumb Dec 30 '14 at 16:09
  • Really? This did work for me: `mvn javadoc:aggregate -Dheader=headerStringAtTheTopRightOfThePage`. You should see the string at the top right corner of every javadoc page with this. – mystarrocks Dec 30 '14 at 16:18
  • What version of the maven-javadoc-plugin are you using? Maybe there's a bug in the latest version. – Dancrumb Dec 30 '14 at 16:20
  • I'm an idiot... see my answer... sorry for wasting your time :( – Dancrumb Dec 30 '14 at 16:48
0

Well, this is embarrassing.

The content was being inserted all along. The issue was that I was using IntelliJ's find function to see if the header injection was working.

Unfortunately, the target directory is excluded from the search index, so those files weren't being searched (even though the folder was explicitly selected).

The code in the question works just fine.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130