I'm new to XML Schema and to JAXB and wondering what the best or expected approach to using the Maven JAXB plugin (http://static.highsource.org/mjiip/maven-jaxb2-plugin/generate-mojo.html)is.
I have a simple XML document format for which I've defined a schema. I'm primarily interested in reading a compliant XML file into Java, but I'll probably also want to add extra properties to the POJOs which won't be in the XML, but will be used at runtime.
By default the plugin places generated code into ${project.build.directory}/generated-sources/xjc. What I think I want to do is copy the generated code into /src/main/java/whatever and add to/modify the code to add my extra properties. When I change the schema, I'd then merge changes form the newly generated POJOs into my own ones.
The alternative is to tell the plugin to place the generated source directly into /src/main/java and to perhaps subclass the POJOs to add my own properties, but I'm not sure whether the marshaling/unmarshaling can still be made to use my extended classes.
Anyone have any guidance on which approach is more normal or what the pitfalls of each are?
In your place I'd leave the generated sources where they are so that the corresponding jar can be built by Maven without further configuration and put your custom code in a different project that depends on the first one. This ensures that everything is build in the right order.
It is your choice whether to derive from the generated classes or just use instances of them in your code, as attributes or, even better, local variables. Personally I'd avoid derivation; after all JAXB is just low level machinery you use to perform I/O in a specific format.
Most importantly: forget about modifying the generated sources; why introduce an error prone manual step in your development process when you can get the same effect automatically?
(To provide a slight variation on to Nicola's answer)
If your schema rarely changes it might make sense to have a completely separate build which just creates the JAXB generated code, jars it, versions it, and sticks it in your repository.
Then in your downstream code you use that jar as a dependency and subclass the JAXB code as necessary to add your new fields.
We went this route because we felt that having JAXB complile every time we did a build was unnecessary as our schemas were pretty static.
Most importantly: forget about modifying the generated sources; why introduce an error prone manual step in your development process when you can get the same effect automatically?
Absolutely.
To elaborate and extend on a point already well-made... if there are a lot of implicit relationships and things you'd like to put "getters" on the JAXB code for, bite the bullet and wrap the JAXB class hierarchy in one that does exactly what you want where you want it.
With IDE-assisted delegation, this is only a little tedious, and factors a lot of straightforward, distracting, low-level code out of your main app.
Another benefit of this is that you'll spend a lot less time fighting JAXB to generate things exactly the way you want - the wrappers will make you care a whole lot less.
Related
I am trying to use the Aspectj compiler ajc in a modular (OSGi setting). The standard way ajc seems to be used is to take aspects & java code and turn it into one a JAR with all classes and resources in the -inpath, -aspectpath, and -sourceroots.
I am trying to weave aspects an OSGi executable JAR from bnd. This executable jar contains a set of bundles that need to be woven. However, in a modular system, the boundary is quite important. For one, the manifest often contains highly relevant information to that bundle or one of the many extenders. Flattening all the classes into a big blog won't work.
I am therefore weaving each bundle separately. However, then the output is cluttered with the aspects. I'd like to import these to keep the aspect modules proper modules. However, using the annotation programming model, I notice that ajc is modifying the aspect modules, so I need to rewrite those as well. This is fine, but since I weave each bundle separately, I have the question if the weaving of the aspect could depend on what gets other modules woven? That is,
does the modification of the annotated aspect depend on the classes that it is woven in?
The other issue is what happens to resources with the same name? Since my -inpath is only one JAR (the bundle), I notice I end up with the correct manifest (META-INF/MANIFEST.MF) in the output. However, if the -inpath consists of many bundles, what will the manifest be? Or any other resource that has the same path and thus overlaps?
Last issue is external dependencies. I understand acj wants to see the whole world and include this whole world into the output JAR. However, I must exclude external dependencies of a bundle. Is there a way to mark JARs as: use, but do not include. A bit like the maven 'provided' scope?
Summary:
Does the modification of an #Aspect annotated class depend on the targets that is applied to?
Can I compile the #Aspect annotated classes into separate JARs?
How to handle the external dependencies that will be provided in the runtime and thus must be excluded from the output JAR.
What are the rules around overlapping resource paths in the -inpath and -sourceroots?
UPDATE In the mean time I've made an implementation in Bndtools.
Does the modification of an #Aspect annotated class depend on the targets that is applied to?
If you want to be 100% sure you have to read the AspectJ source code, but I would assume that an aspect's byte code is independent of its target classes, because otherwise you could not compile aspects separately and also not build aspect libraries.
Can I compile the #Aspect annotated classes into separate JARs?
Absolutely, see above.
How to handle the external dependencies that will be provided in the runtime and thus must be excluded from the output JAR.
If I understand the question correctly, you probably want to put them on the class path during compilation, not on the inpath.
What are the rules around overlapping resource paths in the -inpath and -sourceroots?
Again, probably you have to look at the source code. If I was you I would simply assume that the selection order is undefined and make sure to not have duplicates in the first place. There should be Maven plugins helping you with filtering the way you want the result to be.
bndtools seems to have close ties to Eclipse. So does AspectJ as an Eclipse project. Maybe you can connect with Andy Clement, the AspectJ maintainer. He is so swamped with his day-time job though, he hardly ever has any free cycles. I am trying to unburden him as much as I can, but OSGi is one of my blind spots and I hardly know the AspectJ source code. I am rather an advanced user.
I really don't know how to do what I want 'out of the box', and I think it should be easy(?) ...
Use case :
In development, I have a set of properties which are required. These I should be able to specify in a file which is distributed in the jar. These are used/available to the #Value annotation (which I realise is another level, ...)
In (unit) test, I should be able to override these properties, using the same filename.
In production, I need to be able to gather the ones I want to override from an arbitrary location. When I run my application, I would like these to be merged, with (that sort of) priority order. Basic defaults can then be provided 'in jar', and then overridden by 'local'.
Is there a way to do this (whether in core spring or spring-boot)?
I'm really not looking for a workaround - I'm willing to do a tailored solution for my app - but I'd just like to make sure that I haven't missed an obvious solution
Thx
While creating a new web application using spring-boot, I wondered about a philosophical question: is the best practice to have only one maven module (and so one jar) for both server-side app and static web content of the client (to generate another jar, maybe?), or is it better to have one maven module for server and another one for client-side?
In other words, is it better to have this maven architecture:
MyProject-parent
- MyProject
or this one ?
MyProject-parent
- MyProject-server
- MyProject-html-client
- MyProject-mobile-client
- ...
As I would like to have a browser client and a mobile one, I would be tempted to say it's a good idea to have separate modules. However, using Spring-boot, it's possible to generate a fat jar, i.e. a jar containing all the other ones. As a consequence, the recommended architecture is, I suppose, only one maven module.
So is there a "better" solution? If yes, can you give me arguments?
I'd prefer the multi module approach for the following reasons:
The project is gonna grow and it gets better organized this way,
which means it would be easier to maintain in future and if you break
the modules properly in the beginning, you are not gonna have to lose
time to refactor and split it into different modules later.
It's better "layered”
It's better for bug fix or make new functionalities, since you can
focus on only one module and run the test only for that one.
As you are using Maven, it helps a lot with the dependencies, you can
build it all with one single maven command and it takes care of the
build order. So I can't see any advantage of using single module for
your case.
You might need to release MyProject-mobile-client without releasing
MyProject-html-client any time in future in case you are planning to
use maven release.
My question is really simple, thought I haven't found anything online related to it.
Is the maven-surefire-plugin able to have a dependency excluded but at class level and not at group-id:artifact-id level?
My problem:
I have a web project, related to a plain java project A. This A project contains hundred of classes that do a variety of things (connects to the database, sends web services, calculates stuff, manages the logs, context, sessions, etc...) and the dependency is quite strong. Since all unit testing on the web project will be hard to do without this A project I need to have it as a dependency even for the testing. But of course I cannot use some of the classes in there (mainly the DB connection and the session stuff). So I thought that instead of mocking them (because there are like a few dozens of them and I want to do different things with the functions (like writing to a file that it tried to connect or read the "session" from an xml file)) I can create a A-test project and include the classes I want from this new project and exclude the classes I don't need from the A project.
I know I can copy/paste all the project and then replace the classes I want, but if something changes in project A, I then have to maintain it in A-test (and I am very lazy and don't like to work twice). I thought that maybe I could have some kind of plugin in maven that will copy all the other classes (the one I am not... let say 'overriding'), every time. But it seems a waste of time if I just can tell the plugin which classes to use during the test step.
Thanks for your help!
As far as I understand, main purpose of dependency injection is to have all dependencies separated declaratively, so that we can easily review and change the dependency structure easily...right?
Then by using dependency annotations spread through out the code, aren't we just going back to non-centralized system (similar to simple new operator), which is harder to tweak?
#Autowired/#Inject annotations usually declare dependencies on interfaces rather than on concrete classes (as in the case of new), thus you still can control which implementations should be injected by controlling which beans are declared in the context. Also, these dependencies can be overriden manually.
#Component-family annotations can be controlled as well, since you can exclude particular classes from component scanning.
The purpose of dependency injection is to decouple the declaration of dependencies from the actual satisfying of those dependencies. How the declaration is done is an orthogonal issue.
#Autowired is a form of dependency declaration. Using #Autowired supports encapsulation. A class' injected dependencies are documented directly in the code instead of in another file.
These types of discussions have tendency to become religious so I'll stear clear of the "main purpose" definition and the semantics of whether this or that pattern is really and truly fulfilled.
I try to look at it like a tool that can offer certain features. For instance, using Spring (DI) is a good way to separate interfaces and implementations. Users of a particular interface need not know how to create the implementation (or where it resides). This is often something good. Using Spring also enables a whole lot of other stuff: AOP and AOP-driven features like transaction handling, scopeing and a whole bunch of pre-built integrations to other frameworks and technologies. Annotations make a lot of this easier and clearer and best of all, I don't have to use them where it's not practical or possible - there is always the option to configure it in XML instead.