|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"/> |
| 5 | + <title>Maven, JAXB, and Workarounds</title> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <meta name="description" content=""> |
| 8 | + <meta name="author" content=""> |
| 9 | + <meta name="keywords" content=""> |
| 10 | + <meta name="generator" content="JBake"> |
| 11 | + |
| 12 | + <!-- Le styles --> |
| 13 | + <link href="../../css/bootstrap.min-3.3.7.css" rel="stylesheet"> |
| 14 | + <link href="../../css/asciidoctor.css" rel="stylesheet"> |
| 15 | + <link href="../../css/base.css" rel="stylesheet"> |
| 16 | + <link href="../../css/prettify.css" rel="stylesheet"> |
| 17 | + |
| 18 | + <script> |
| 19 | + (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| 20 | + (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| 21 | + m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| 22 | + })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); |
| 23 | + |
| 24 | + ga('create', 'UA-99754303-1', 'auto'); |
| 25 | + ga('send', 'pageview'); |
| 26 | + </script> |
| 27 | + |
| 28 | + <!-- HTML5 shim, for IE6-8 support of HTML5 elements --> |
| 29 | + <!--[if lt IE 9]> |
| 30 | + <script src="../../js/html5shiv.min.js"></script> |
| 31 | + <![endif]--> |
| 32 | + |
| 33 | + <!-- Fav and touch icons --> |
| 34 | + <!--<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png"> |
| 35 | + <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png"> |
| 36 | + <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png"> |
| 37 | + <link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">--> |
| 38 | + <link rel="shortcut icon" href="../../favicon.png"> |
| 39 | + </head> |
| 40 | + <body onload="prettyPrint()"> |
| 41 | + <div id="wrap"> |
| 42 | + |
| 43 | + <!-- Fixed navbar --> |
| 44 | + <div class="navbar navbar-default navbar-fixed-top" role="navigation"> |
| 45 | + <div class="container"> |
| 46 | + <div class="navbar-header"> |
| 47 | + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> |
| 48 | + <span class="sr-only">Toggle navigation</span> |
| 49 | + <span class="icon-bar"></span> |
| 50 | + <span class="icon-bar"></span> |
| 51 | + <span class="icon-bar"></span> |
| 52 | + </button> |
| 53 | + <a class="navbar-brand" href="../../">Heroicode</a> |
| 54 | + </div> |
| 55 | + <div class="navbar-collapse collapse"> |
| 56 | + <ul class="nav navbar-nav"> |
| 57 | + <li><a href="../../index.html">Home</a></li> |
| 58 | + <li><a href="../../about.html">About</a></li> |
| 59 | + <li><a href="../../resume.html">Résumé</a></li> |
| 60 | + <li><a href="../../feed.xml">Subscribe</a></li> |
| 61 | + </ul> |
| 62 | + </div><!--/.nav-collapse --> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + <div class="container"> |
| 66 | + <div class="page-header"> |
| 67 | + <h1>Maven, JAXB, and Workarounds</h1> |
| 68 | + </div> |
| 69 | + |
| 70 | + <p><em>10 November 2008</em></p> |
| 71 | + |
| 72 | + <p><p>We’re using Maven 2 at work to build our projects now. One of the projects uses JAXB to manage some XML documents. Well, the JAXB standard doesn’t say anything about how namespaces are handled when marshalling to XML, which means I can’t write XSLT against our output (FAIL). Sun’s JAXB implementation has a way to control namespace output, but it’s non-standard. Even worse, it changed between Java 5.0 and 6.0 (package rename), so code written against 5.0 will fail when running in a newer JVM.</p> |
| 73 | +<p>I’m <a target="_blank" |
| 74 | +href="http://pragmaticintegration.blogspot.com/2007/11/moving-jaxb-20-applications-built-by.html" |
| 75 | +>not the only one</a> who has had this problem, but I think my solution is different than any I’ve seen. It took me a long time to get Maven to cooperate, but I finally got it working. I believe I found a bug in Maven, but I haven’t looked at the source yet to verify it. Maven documentation is extremely lacking when it comes to explaining what’s really going on. Sadly, it’s the best thing out there; the only good thing I can say is Maven 2 is better than the original.</p> |
| 76 | +<p>Here is the Java code and Maven POM excerpts necessary to explain my solution:</p> |
| 77 | +<pre><code class="java">import com.sun.xml.bind.marshaller.NamespacePrefixMapper; |
| 78 | +class PrefixMapper_JDK5 extends NamespacePrefixMapper { |
| 79 | + @Override |
| 80 | + public String getPreferredPrefix(String namespaceUri, String suggestion, |
| 81 | + boolean requirePrefix) { |
| 82 | + return suggestion; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +//JDK 6 added "internal" to the package naming |
| 87 | +import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper; |
| 88 | +class PrefixMapper_JDK6 extends NamespacePrefixMapper { |
| 89 | + @Override |
| 90 | + public String getPreferredPrefix(String namespaceUri, String suggestion, |
| 91 | + boolean requirePrefix) { |
| 92 | + return suggestion; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +public Object getNamespacePrefixMapper() { |
| 97 | + Object r = null; |
| 98 | +//the mapper classes should be compiled separately before this method |
| 99 | +//is compiled so they match the proper JDK (-source and -target flags) |
| 100 | + Class[] mapperList = { |
| 101 | + //first one in list gets priority... |
| 102 | + PrefixMapper_JDK6.class, PrefixMapper_JDK5.class |
| 103 | + }; |
| 104 | + try { //there is a better way to implement this... you can figure it out |
| 105 | + r = mapperList[0].getClass().newInstance(); |
| 106 | + } catch (Exception e) { |
| 107 | + try { |
| 108 | + r = mapperList[1].getClass().newInstance(); |
| 109 | + } catch (Exception ex) {} |
| 110 | + } |
| 111 | + return r; //null if neither class could be loaded |
| 112 | +} |
| 113 | +</code></pre> |
| 114 | +<pre><code class="xml"><plugin> |
| 115 | + <groupId>org.apache.maven.plugins</groupId> |
| 116 | + <artifactId>maven-compiler-plugin</artifactId> |
| 117 | + <configuration> |
| 118 | + <source>1.6</source> |
| 119 | + <target>1.6</target> |
| 120 | + <excludes> |
| 121 | + <!-- Maven is buggy here and will not remove the first |
| 122 | + exclude line given, so the first one needs to be |
| 123 | + bogus. --> |
| 124 | + <exclude>==MavenBugWorkaround==</exclude> |
| 125 | + <exclude>**/PrefixMapper_JDK5.java</exclude> |
| 126 | + </excludes> |
| 127 | + </configuration> |
| 128 | + <executions> |
| 129 | + <execution> |
| 130 | + <goals> |
| 131 | + <goal>compile</goal> |
| 132 | + </goals> |
| 133 | + <phase>compile</phase> |
| 134 | + <configuration> |
| 135 | + <source>1.5</source> |
| 136 | + <target>1.5</target> |
| 137 | + <!-- clears inherited exclude list |
| 138 | + (except for 1st element) --> |
| 139 | + <excludes/> |
| 140 | + <!-- only uncompiled files will be looked at --> |
| 141 | + <includes> |
| 142 | + <include>**/*.java</include> |
| 143 | + </includes> |
| 144 | + </configuration> |
| 145 | + </execution> |
| 146 | + </executions> |
| 147 | +</plugin> |
| 148 | +</code></pre></p> |
| 149 | + |
| 150 | + <hr /> |
| 151 | + |
| 152 | + </div> |
| 153 | + <div id="push"></div> |
| 154 | + </div> |
| 155 | + |
| 156 | + <div id="footer"> |
| 157 | + <div class="container"> |
| 158 | + <p class="muted credit">© 2017 | Mixed with <a href="http://getbootstrap.com/">Bootstrap v3.3.7</a> | Baked with <a href="http://jbake.org">JBake v2.5.1</a></p> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + |
| 162 | + <!-- Le javascript |
| 163 | + ================================================== --> |
| 164 | + <!-- Placed at the end of the document so the pages load faster --> |
| 165 | + <script src="../../js/jquery-1.11.1.min.js"></script> |
| 166 | + <script src="../../js/bootstrap.min-3.3.7.js"></script> |
| 167 | + <script src="../../js/prettify.js"></script> |
| 168 | + |
| 169 | + </body> |
| 170 | +</html> |
0 commit comments