How to include Google Analytics snippet in Javadoc

2019-07-16 19:12发布

问题:

I would like to include the Google Analytics Javascript code in the head element of my generated Javadoc HTML output. How can I do this?

I figured I may need to write a custom Doclet, but this is probably going to be a nightmare of a learning curve. Isn't there a simpler way?

回答1:

You have 2 solutions, Using maven plugin Javadoc with

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <configuration>
                    <header>&lt;b&gt;test test&lt;/b&gt</header>    
            </configuration>
        </plugin>
    </plugins>
</reporting>

you change my "test test" with your google analytics script and don't forget to change < with < and > with > and then you call

mvn clean javadoc:javadoc

or using the old method http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#header with your html script



回答2:

Use CData

<configuration>
    <!-- GA Tracking code -->
    <header>
    <![CDATA[
        <script type="text/javascript">
          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
          _gaq.push(['_trackPageview']);

          (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
          })();

        </script>
    ]]>
    </header>
</configuration>