I have a complex structure of maven module with involves some like 20 modules for a standard application. The standard application is also supposed to contain spring xml files and packages folder setup from the start. Right now we are copying a example application and change the name all over it. But this seems like a lot of unnecessary work. I'm wondering what software would be best to use generate this.
I saw that maven had something for this called archetype but i'm not find much documentation and example on how to use it. Maybe there is better tool for this task.
Related
I'm using Maven Java API to configure Maven in a custom Java project.
In particular I need to configure some Maven settings, among which there are proxy settings.
How can i do this? I googled a lot, but I found no examples on how to use Maven from Java.
Can You give me an example or a guide, a snippet of code, whatever you want to clarify HOW TO USE (AND CONFIGURE) Maven by Java API, i.e from Java code?
I found this maven reference, but what do I specifically need?
Thanks in advance.
I've already seen this question, but unfortunately there is no mention on how to edit settings.xml from maven api, I suppose it is possible, but I'm not sure of it, so I asked a new question, wider than that one, how can I manage Maven from Java? settings, run, properties, whatever... is it possible?
For example, about settings management, I found this API maven-settings, it can be useful? It's "read-only" API? I guess it isn't, but I've found no way how to "write" modifications to file, there are no examples on how to use it.
Well, yes, you are a bit crazy. You can take a look at some plug-ins which modify pom.xml files. For example, the versions-set facility shown here:
http://www.mojohaus.org/versions-maven-plugin/set-mojo.html
The source code for that plug-in will show you how to modify pom.xml files, but you also want to modify the settings.xml file.
All of these files are XML. Basically, you want to obtain a DOM for the .xml file. So, you can use generic XML tools to (1) read the file, (2) modify the document model, (3) write the data back to disk.
Note well: Maven caches the .xml files. You have to stop the maven executable and restart it to force it to re-read the .xml files. It sounds like you'll probably be doing this as a matter of course. :-)
I have two java projects that are fairly independent beside the fact that they share a common mysql database.
I wanted to refactor these project and extract everything regarding the common data layer. I am using jOOQ, so most of this layer gets autogenerated in my build. Beside that i then have a few common entity classes that are used in both projects.
what would be the best practice to separate this, so that any change can be done one place and still propagate to both projects? create a third java simple project with the common code? what would you do
I work on a distributed system, and multiple daemons need access to the same Postgres database via jOOQ. Since each daemon is its own Java project, I am in the same boat as you basically.
The solution I've been using is to create a third Java project as a Java Library. If you're using Netbeans you can just include it as a subproject dependency and any changes to the library project can be recompiled into the individual application projects.
One thing of note, you'll need to specify the jOOQ library jars in all 3 projects. In Netbeans its easy to specify a project's library directory, and have multiple projects share these dependencies. Netbeans will copy the dependencies at deployment time.
Edit:
The steps are basically:
create a master layout for system, IE:
/master-project/
/master-project/library
/master-project/software
/master-project/software/daemon1
/master-project/software/daemon2
/master-project/common
/master-project/common/utility1
/master-project/common/utility2
create third-party "library" bundles of {jar,src,docs} under /master-project/library.
create "application" projects under /master-project/software, making sure to tell Netbeans to only use third-party libraries under /master-project/library.
create "library" projects under /master-project/common, making sure to tell NB only to use third-party libraries under /master-project/library.
create a "library" for jOOQ code to be shared, as in step 4.
Each project is responsible for its own compile script (including generating jOOQ code, if desirable), and correctly specifying its dependencies out of /master-project/library, and /master-project/common.
we are trying to develop a web application framework and build implementatins on top of it. This framwork will be versioned in SVN, live its own life in parallel to those implementations. It will have lots of spring config files, security config and so on. We would like to use those in those implementations.
What structure should such an project have? Keep everything together? Link particular folers (implementations) in "svn: externals"? We would like to use Maven, and create an archetype for those implementations, but is it possible to update the archetype after it has been changed in implementation applications?
Regards,
This is a good example :
http://www.sonatype.com/books/mvnex-book/reference/web.html
Also this book is very useful resource when starting with maven
I found this also :
http://www.avajava.com/tutorials/lessons/how-do-i-create-a-web-application-project-using-maven.html
I'd suggest you create your framework project as a simple jar project to include in your implementation, which would be war projects. For the Spring config files you have three options then:
Package them into your framework jar. This would make it hard for the implementations to customize it. I would not recommend it, unless your configuration is definitively fixed.
Use svn: externals. I have not much experience with that, but I think dependencies between svn repositories would be hard to manage.
Maintain these configuration files per implementation. So, an archetype would help to get started with an initial configuration. Then maintain these configuration files as your framework evolves. This is what we do most of the time. The good thing about Spring configuration is that it often rarely needs to be touched once you are confident with it.
Is it true that the only configuration file I need to bother with is pom.xml ?
It seems to me that Java guys live xml more than any other stuff,is that true?
Maven uses XML for POM files because XML is a good choice for describing things ... and a POM file is a description of a project. I supposed that the Maven implementors could have invented a custom language, but then lots of people would have complained about having to learn another language syntax.
It would make no sense to express Maven POM files in some kind of scripting language because they are not "scripts" in any recognizable sense.
It seems to me that Java guys live xml more than any other stuff,is that true?
No. "Java guys" use all sorts of technologies for configuring things: XML, JSON, properties files, databases, bespoke languages, you name it someone has used it.
If your problem is that you hate using XML, either:
get over it,
get an editor that hides the XML syntax from you,
use (shudder) Make,
use (whimper) build scripts written in some clunk old scripting language, or
go away and implement a better solution.
But I'll stick with Maven, thanks.
Is it true that the only configuration file I need to bother with is pom.xml ?
Yes, Maven uses a pom.xml file to describe a project's medata and extend Maven's behavior, which also known as the Project Object Model (POM) file.
Maven will also look by default for a per user configuration file in ~/.m2/settings.xml but this file is optional.
Why doesn't maven use scripts for this kind of job, I don't see xml has any advantage here, which is why I don't like ant
What should Maven have used in 2002 if not XML? Makefiles? In my opinion, XML was and still is a very decent choice for a declarative Project Object Model. Yes XML is verbose but it is perfect for the job and allows to benefit from nice things like validation, transformation, code completion which seem important to me.
Now, nothing forces you to use Maven if you don't like it and feel free to use Gradle or Gant or Buildr (if the size of their communities is not a concern).
Just in case, note that Maven 3 offers polyglot support (see this post and this one) but as I said, writing a pom in Groovy, YAML, etc doesn't only have advantages (e.g. validation).
You probably want to give some attention to the settings.xml file too. However, for the most part you are correct, everything is in the pom.xml
How do you determine what jars are needed for such and such feature of a framework? For example, what jars would be needed out of all those available for Spring in order to support only dependency injection?
There are tools that create minimal JARs by figuring out which classes are actually used in an application by statically analyzing the code, then creating a new JAR containing only those classes. (I recall using Zelix Classmaster to do this, but there are many alternatives.)
The problem with using these tools for a DI framework like Spring include:
The existing only trace static dependencies. If you dynamically load classes, you have to specifically tell the analyser about each one. DI frameworks in general, and Spring in particular is replete with dynamic loading, including dynamic loading that is opaque to application code.
The existing tools work by creating a new output JAR, not by telling you which of the input JARs are not used. While repackaging the JARs is OK if you are creating a shrink-wrapped application from a closed-source codebase, it is undesirable in general, and potentially problematic with some open-source licenses. Certainly you don't want to do this with Spring.
In theory, someone could write a tool to help. In practice, the tool would need to (for example) know how to extract dynamic class dependencies from Spring configurations expressed in annotations, XML and from bean descriptors created at runtime from higher order configuration (SpringSecurity does this for example). That is a big ask. And even then you have the problem that a "small" change to the wirings made on the installation platform could fail due to a required JARs having been left out by the JAR pruning process.
In my view, the more practical alternatives are:
If you use Maven / Ivy to manage your dependencies, look at the dependency graphs, strip out dependencies that appear to be no longer needed ... and test, test, test.
Manually strip out JARs that appear to be unused ... and test, test, test.
Don't worry about it. A moderate level of unused JAR cruft might add a second or three to deployment and webapp startup times, but that generally doesn't matter. (But if it does ... see above.)
This is why some older Java projects end up having 600 Jars and a 200 MB war file, for a 10,000 line application. Kind of a pain if you don't manage it carefully...
You should really ask the framework provider or read the documentation. Statically analyzing what jars are required might not be enough in some cases(dynamic loading) and sometimes you might end up with too many jars.
I once did some ftp helper stuff to a sort of "utility" library. It depended on some apache ftp jar. If you never used the ftp features in the library you would not need the ftp jar but statical analysis of the code might say you need it. This is something you should documents.