Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Wednesday, November 9, 2011

Building from source BPM Console (for JBPM4)

For some personal reasons I wanted to customize the JBPM Console, so the only way to do it is by building from source code.
The first thing I had to do is get the sources and compile them using Maven :)

If by any chance you're having a hard time with this error (like me)
[ERROR] Failed to execute goal on project gwt-console: Could not resolve dependencies for project org.jboss.bpm:gwt-console:war:2.2.3-Final: The following artifacts could not be resolved: org.timepedia.chronoscope:chronoscope:jar:2.0_jboss-SNAPSHOT, org.timepedia.chronoscope:chronoscope-api:jar:2.0_jboss-SNAPSHOT: Failure to find org.timepedia.chronoscope:chronoscope:jar:2.0_jboss-SNAPSHOT in https://repository.jboss.org/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of jboss-public-repository-group has elapsed or updates are forced -> [Help 1]

Just do as I did. Go to Maven Hub and search for your missing jar, on my case I found it on Thirdparty-uploads repository (from JBoss);

Unfortunately, the version I was looking for (according to the previous error) was chronoscope:jar:2.0_jboss-SNAPSHOT and I found 2.0_jboss, I guess you know what to do now, don't you?

Just cheat Maven by installing on your local repository version "2.0_jboss" as "2.0_jboss-SNAPSHOT"

Voila!!!

As result you should get these 4 artifacts:
  • Console UI: gwt-console.war
  • Console Server: gwt-console-server.war
  • Domain model: gwt-console-rpc.jar
  • Integration Layer: gwt-console-server-integration.jar

Also, don't forget to include the bpaf-explorer jars. To avoid this:
14:52:32,728 [main] ERROR [ContainerBase] StandardWrapper.Throwable
com.google.inject.ProvisionException: Guice provision errors:

1) Error injecting constructor, java.lang.NoClassDefFoundError: org/jboss/bpm/monitor/model/metric/Timespan
  at org.jboss.errai.bus.server.service.ErraiServiceImpl.(ErraiServiceImpl.java:50)
  while locating org.jboss.errai.bus.server.service.ErraiServiceImpl
  while locating org.jboss.errai.bus.server.service.ErraiService

1 error
 at com.google.inject.InjectorImpl$4.get(InjectorImpl.java:767)
 at com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:793)
 at org.jboss.errai.bus.server.servlet.AbstractErraiServlet.buildService(AbstractErraiServlet.java:117)
 at org.jboss.errai.bus.server.servlet.AbstractErraiServlet.init(AbstractErraiServlet.java:88)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)


Wednesday, February 9, 2011

About Hibernate 3 plugin for Maven

In one of my previous entries, I had something really cool on my POM, which basically generated the DataBase based on my Entyties (or Domain objects), and that was the Hibernate 3 plugin for Maven
As you will see on the official documentation for that plugin, you can do a bunch of things, but it doesn't says anything about, generating Java classes based on the Tables, right? In other words, if I want my model java files (obtained by reveng) compiled, you don't need to run hbm2hbmxml.
I found the solution to this issue (unfortunately is not documented, and that's why you see it here):

Plugin configuration
hibernate.properties
hibernate.connection.username=root
hibernate.connection.password=secretit0
hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/little_store
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.default_schema=little_store

model_reveng.xml
Then the last step is executing Maven:
mvn hibernate3:hbm2java

And we're ready to roll! :)

Tuesday, December 7, 2010

Excluding xerces/xercesImpl from your CXF project

I'm not sure how much time I spent trying to figure out why my new service was not working, if I followed the same steps I usually follow on all my past CXF apps;
The only difference was to include a new dependency which has more business logic.

Then comparing my last CXF project lib folder with my new one, I found xercesImpl*.jar was additional to my current project.
I just removed from my POM (added as exclusion from my new dependency) and Listo! my CXF Web Service is working as expected :D

Sunday, June 13, 2010

WebServices with CXF - Integrating with CXF II

This is going to be a really small entry, because we're going to finish with my ServiceImplementation.
On the next entries we'll discuss configuration files & finally we'll see everything working (Obviously I'll need to test it).

But before that, let's analyze my implementation:

As I've been doing I'm using a lot of WebServices Annotations

What I did???

  • Define service name of the Web Service
  • Declare the complete name of the service endpoint interface defining the service's abstract Web Service contract (My Interface)
  • And set my targetNamespace: the targetNamespace is used for the namespace for the wsdl:portType (and associated XML elements)

WebServices with CXF - Integrating with CXF I

As I said before on Service Interface & Implementation, somehow we'll have to retake that code to implement CXF, and that's what we're going to do.
I want to expose as WebService my LibraryService and as WebMethods these methods:

  • public List getAllAuthors()
  • public List getAllBooks()
  • public List getAllEditors()
  • public List getAllOnlyEditors()
  • public BookDTO getBook(Integer bookId)
Ok, everything looks nice, but what happens if my client is asking for a "subService" (WebMethod) which returns AllAuthors, but he/she doesn't expect a WebMethod called getAuthors, retrieveAuthors or returnAuthors.
What can we do??? -We're done with our coding and that method (getAllAuthors) is being used in many places in our App.
Are you going to rename your code just because your client doesn't like your naming convention???. Later you'll see my answer.

Let's modify my code, first, Interface then Implementation;
By adding Java WebServices API Annotations And don't forget for every method we must declare it like a WebMethod In this case I'm declaring a couple of additional attributes, which basically let me give another name to WebMethod, a different name to my original Java Method - this will be the name available on my WebService/WebMethod (this is the second option for my client)

And finally, what can I do if have logic on this interface that I don't want to make it public (available as WebMethod)???
This is the answer: "Just exclude it"

Sunday, May 23, 2010

WebServices with CXF - My Service interface & Impl

Like with our DTO's, later this code will be having some changes when Apache CXF is called to scene.

Now this is my implementation, as you see I'm becoming fan of the use of annotations. I feel like "NIÑO CHIQUITO" (child) with new toy.

In future entries I'll explain the configuration files involved to attach together Hibernate, Spring and Apache CXF.
But for now, let'f finish with Hibernate+Spring part and then we'll integrate with Apache CXF (then we'll talk about confuration files)

Hasta luego!

WebServices with CXF - My DTO's

For your DTO's youmust apply the same rule like with every Java Bean (getter's & setter's, toString, hashCode, equals & Serialization)

My first step is creating a base DTO with all common fields and methods, then it's just extending from that class. I'll just put 2 examples, Ok?

Later we'll have to retake this objects, because we'll have add some extra lines (anotations) just to make them compatible with our WebServices framework, it will be fun.

Saturday, April 17, 2010

WebServices with CXF - My Pojos & DAOs

First thing, is a good practice declaring methods equals and hashCode
You know what, I'll skip these methods on my code, eclipse can do it for you, don't be lazy!!!
And one more thing, as Architect it's a must declaring toString method, because when you're debugging these method it's going to be really helpful, and you know what... declare toString it's a must too.
The next step is explain what are those annotations, well they're Java 6 annotations, so we can skip some configurations and declare them into our code. I'll try to explain as simple as possible these annotations, let's analyze them:
What I'm doing here is just declaring that this POJO will be an Entity which maps to a table called EDITOR

Using Annotations, I declared that editorId is my PK which basically is AutoIncremental and to which column will be mapped. With columns is really easy.
But what about relations??? Like
BOOK >-- EDITOR
In this case I just declared what I needed to declare: One Editor has Many books using a List of Book, like in every mapping of Hibernate: the most common fetching strategy to be used will be LAZY; basically what I did was linking the current ENTITY by it's PK (mappedBy="editor") to Book
So in Book this is what we have to do (the inverse to OneToMany and define an Editor for each Book, so each child will have a reference to it's Dad):

The last thing is just declare which column & attribute will be used to do the JOIN between child and father


You can get more details if you look for that topic in Hibernate official documentation.
And these are my DAO's (I just included 1 as example), others should be similar. Always code oriented to interfaces! Later you'll thank me. In my next entry I will explain my Services and we'll be ready for using CXF.

Saturday, April 3, 2010

WebServices with CXF - Preparing Worskpace - POM

I'm going to use the perfect combination: Spring + Spring MVC + Hibernate + more fancy stuff The first step is creating my pom.xml, and there you go:

If you read carefully my POM, you'd found something interesting. Or maybe not. Whatever!

  • I grouped my Spring & Hibernate dependencies other external pom.xml, I know, I already explained that concept before
  • What's going on with that plug-in if we already have the database built? Interesting question, but "In the beginning there was nothing. Then JuCa said: I just want to write Code, and I'm almost done with my POJO's". Then based on my pojo's I could create the tables and it's relations-referential integrity and some indexes :D

How could I achieve that? So simple. Let's code our Pojos and then I'll explain this: Based on our DDL's let's code!!!

WebServices with CXF - Our Database

Too many times I tried to look for some good examples for my padawans, but as always, incomplete stuff or not really clear. Anyway, that's why I'm going to develop a small app using a bunch of useful things, and one of these it's going to be Apache CXF The first and most important step is what kind of problem I'm going to solve using the app? Well, let's create a scenario: I have a small Database on MySQL 5.1.37. this is my schema: mysql> desc author; mysql> desc author_book; mysql> desc book; mysql> desc editor;

As you noticed the relations are like this:

AUTHOR --< AUTHOR_BOOK >-- BOOK >-- EDITOR

Thursday, November 5, 2009

Grouping Maven Dependencies

Maven can be used to manage everything from simple, single-project systems to builds that involve hundreds of inter-related submodules. Part of the learning process with Maven isn’t just figuring out the syntax for configuring Maven, it is learning the “Maven Way”—the current set of best practices for organizing and building projects using Maven. This is the first in a series of posts that will attempt to distill some of this knowledge to help you adopt best practices from the start without having to wade through years of discussions on the Maven mailing lists.

Grouping Dependencies

If you have a set of dependencies which are logically grouped together. You can create a project with pom packaging that groups dependencies together. For example, let’s assume that your application uses Hibernate, a popular Object-Relational mapping framework. Every project which uses Hibernate might also have a dependency on the Spring Framework and a MySQL JDBC driver. Instead of having to include these dependencies in every project that uses Hibernate, Spring, and MySQL you could create a special POM that does nothing more than declare a set of common dependencies. You could create a project called persistence-deps (short for Persistence Dependencies), and have every project that needs to do persistence depend on this convenience project:

If you create this project in a directory named persistence-deps, all you need to do is create this pom.xml and run mvn install. Since the packaging type is pom, this POM is installed in your local repository. You can now add this project as a dependency and all of its dependencies will be added to your project. When you declare a dependency on this persistence-deps project, don’t forget to specify the dependency type as pom.

If you later decide to switch to a different JDBC driver (for example, JTDS), just replace the dependencies in the persistence-deps project to use net.sourceforge.jtds:jtds instead of mysql:mysql-java-connector and update the version number. All projects depending on persistence-deps will use JTDS if they decide to update to the newer version. Consolidating related dependencies is a good way to cut down on the length of pom.xml files that start having to depend on a large number of dependencies. If you need to share a large number of dependencies between projects, you could also just establish parent-child relationships between projects and refactor all common dependencies to the parent project, but the disadvantage of the parent-child approach is that a project can have only one parent. Sometimes it makes more sense to group similar dependencies together and reference a pom dependency. This way, your project can reference as many of these consolidated dependency POMs as it needs.
Maven uses the depth of a dependency in the tree when resolving conflicts using a nearest-wins approach. Using the dependency grouping technique above pushes those dependencies one level down in the tree. Keep this in mind when choosing between grouping in a pom or using dependencyManagement in a parent POM

Optimizing with the Maven Dependency Plugin

On larger projects, additional dependencies often tend to creep into a POM as the number of dependencies grow. As dependencies change, you are often left with dependencies that are not being used, and just as often, you may forget to declare explicit dependencies for libraries you require. Because Maven 2.x includes transitive dependencies in the compile scope, your project may compile properly but fail to run in production. Consider a case where a project uses classes from a widely used project such as Jakarta Commons BeanUtils. Instead of declaring an explicit dependency on BeanUtils, your project simply relies on a project like Hibernate that references BeanUtils as a transitive dependency. Your project may compile successfully and run just fine, but if you upgrade to a new version of Hibernate that doesn’t depend on BeanUtils, you’ll start to get compile and runtime errors, and it won’t be immediately obvious why your project stopped compiling. Also, because you haven’t explicitly listed a dependency version, Maven cannot resolve any version conflicts that may arise.

A good rule of thumb in Maven is to always declare explicit dependencies for classes referenced in your code. If you are going to be importing Commons BeanUtils classes, you should also be declaring a direct dependency on Commons BeanUtils. Fortunately, via bytecode analysis, the Maven Dependency plugin is able to assist you in uncovering direct references to dependencies. Using the updated POMs we previously optimized, let’s look to see if any errors pop up:

$ mvn dependency:analyze
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO]   Chapter 8 Simple Parent Project
[INFO]   Chapter 8 Simple Object Model
[INFO]   Chapter 8 Simple Weather API
[INFO]   Chapter 8 Simple Persistence API
[INFO]   Chapter 8 Simple Command Line Tool
[INFO]   Chapter 8 Simple Web Application
[INFO]   Chapter 8 Parent Project
[INFO] Searching repository for plugin with prefix: 'dependency'.

...

[INFO] ------------------------------------------------------------------------
[INFO] Building Chapter 8 Simple Object Model
[INFO]    task-segment: [dependency:analyze]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing dependency:analyze
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [dependency:analyze]
[WARNING] Used undeclared dependencies found:
[WARNING]    javax.persistence:persistence-api:jar:1.0:compile
[WARNING] Unused declared dependencies found:
[WARNING]    org.hibernate:hibernate-annotations:jar:3.3.0.ga:compile
[WARNING]    org.hibernate:hibernate:jar:3.2.5.ga:compile
[WARNING]    junit:junit:jar:3.8.1:test

...

[INFO] ------------------------------------------------------------------------
[INFO] Building Chapter 8 Simple Web Application
[INFO]    task-segment: [dependency:analyze]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing dependency:analyze
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [dependency:analyze]
[WARNING] Used undeclared dependencies found:
[WARNING]    org.sonatype.mavenbook.optimize:simple-model:jar:1.0:compile
[WARNING] Unused declared dependencies found:
[WARNING]    org.apache.velocity:velocity:jar:1.5:compile
[WARNING]    javax.servlet:jstl:jar:1.1.2:compile
[WARNING]    taglibs:standard:jar:1.1.2:compile
[WARNING]    junit:junit:jar:3.8.1:test

In the truncated output just shown, you can see the output of the dependency:analyze goal. This goal analyzes the project to see whether there are any indirect dependencies, or dependencies that are being referenced but are not directly declared. In the simple-model project, the Dependency plugin indicates a “used undeclared dependency” on javax.persistence:persistence-api. To investigate further, go to the simple-model directory and run the dependency:tree goal, which will list all of the project’s direct and transitive dependencies:


$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Chapter 8 Simple Object Model
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree]
[INFO] org.sonatype.mavenbook.optimize:simple-model:jar:1.0
[INFO] +- org.hibernate:hibernate-annotations:jar:3.3.0.ga:compile
[INFO] |  - javax.persistence:persistence-api:jar:1.0:compile
[INFO] +- org.hibernate:hibernate:jar:3.2.5.ga:compile
[INFO] |  +- net.sf.ehcache:ehcache:jar:1.2.3:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.0.4:compile
[INFO] |  +- asm:asm-attrs:jar:1.5.3:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- cglib:cglib:jar:2.1_3:compile
[INFO] |  +- asm:asm:jar:1.5.3:compile
[INFO] |  - commons-collections:commons-collections:jar:2.1.1:compile
[INFO] - junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

From this output, we can see that the persistence-api dependency is coming from hibernate. A cursory scan of the source in this module will reveal many javax.persistence import statements confirming that we are, indeed, directly referencing this dependency. The simple fix is to add a direct reference to the dependency. In this example, we put the dependency version in simple-parent’s dependencyManagement section because the dependency is linked to Hibernate, and the Hibernate version is declared here. Eventually you are going to want to upgrade your project’s version of Hibernate. Listing the persistence-api dependency version near the Hibernate dependency version will make it more obvious later when your team modifies the parent POM to upgrade the Hibernate version.

If you look at the dependency:analyze output from the simple-web module, you will see that we also need to add a direct reference to the simple-model dependency. The code in simple-webapp directly references the model objects in simple-model, and the simple-model is exposed to simple-webapp as a transitive dependency via simple-persist. Since this is a sibling dependency that shares both the version and groupId, the dependency can be defined in simple-webapp’s pom.xml using the ${project.groupId} and ${project.version}.

How did the Maven Dependency plugin uncover these issues? How does dependency:analyze know which classes and dependencies are directly referenced by your project’s bytecode? The Dependency plugin uses the ObjectWeb ASM (http://asm.objectweb.org/) toolkit to analyze the raw bytecode. The Dependency plugin uses ASM to walk through all the classes in the current project, and it builds a list of every other class referenced. It then walks through all the dependencies, direct and transitive, and marks off the classes discovered in the direct dependencies. Any classes not located in the direct dependencies are discovered in the transitive dependencies, and the list of “used, undeclared dependencies” is produced.

In contrast, the list of unused, declared dependencies is a little trickier to validate, and less useful than the “used, undeclared dependencies.” For one, some dependencies are used only at runtime or for tests, and they won’t be found in the bytecode. These are pretty obvious when you see them in the output; for example, JUnit appears in this list, but this is expected because it is used only for unit tests. You’ll also notice that the Velocity and Servlet API dependencies are listed in this list for the simple-web module. This is also expected because, although the project doesn’t have any direct references to the classes of these artifacts, they are still essential during runtime.

Be careful when removing any unused, declared dependencies unless you have very good test coverage, or you might introduce a runtime error. A more sinister issue pops up with bytecode optimization. For example, it is legal for a compiler to substitute the value of a constant and optimize away the reference. Removing this dependency will cause the compile to fail, yet the tool shows it as unused. Future versions of the Maven Dependency plugin will provide better techniques for detecting and/or ignoring these types of issues.

You should use the dependency:analyze tool periodically to detect these common errors in your projects. It can be configured to fail the build if certain conditions are found, and it is also available as a report.

Describing Maven Plugins with help:describe option

If you do any non-trivial customization of a Maven build, you’ll understand that you need to have a good understanding of Maven Plugin configuration. What configuration parameters are available for a particular plugin goal? Most users tend to Google for the particular Maven plugin and rely on the fact that most Maven-generate plugin sites contain a standard list of goals and configuration parameters. While this works, there is a better way: use the Maven Help plugin’s describe goal to list the available configuration parameters for a Maven plugin.

Describing Plugin Configuration Parameters with a Command
$ mvn help:describe -Dcmd=compiler:compile
[INFO] [help:describe {execution: default-cli}]
[INFO] 'compiler:compile' is a plugin goal (aka mojo).
Mojo: 'compiler:compile'
compiler:compile
  Description: Compiles application sources
  Deprecated. No reason given

Sure, the compile goal in the compiler plugin will compile application sources. That’s not exactly what you were looking for, what you usually need is a detailed list of plugin configuration parameters. You can get this by passing in a -Ddetail argument.

$mvn help:describe -Dcmd=compiler:compile -Ddetail
[INFO] [help:describe {execution: default-cli}]
[INFO] 'compiler:compile' is a plugin goal (aka mojo).
Mojo: 'compiler:compile'
compiler:compile
  Description: Compiles application sources
  Deprecated. No reason given
  Implementation: org.apache.maven.plugin.CompilerMojo
  Language: java
  Bound to phase: compile

Available parameters:

compilerArgument
  Sets the unformatted argument string to be passed to the compiler if fork
  is set to true.

  This is because the list of valid arguments passed to a Java compiler
  varies based on the compiler version.
  Deprecated. No reason given

compilerArguments
  Sets the arguments to be passed to the compiler (prepending a dash) if
  fork is set to true.

  This is because the list of valid arguments passed to a Java compiler
  varies based on the compiler version.
  Deprecated. No reason given

compilerId (Default: javac)
  The compiler id of the compiler to use. See this guide for more
  information.
  Deprecated. No reason given

compilerVersion
  Version of the compiler to use, ex. '1.3', '1.5', if fork is set to true.
  Deprecated. No reason given

debug (Default: true)
  Set to true to include debugging information in the compiled class files.
  Deprecated. No reason given

encoding
  The -encoding argument for the Java compiler.
  Deprecated. No reason given

excludes
  A list of exclusion filters for the compiler.
  Deprecated. No reason given

executable
  Sets the executable of the compiler to use when fork is true.
  Deprecated. No reason given

failOnError (Default: true)
  Indicates whether the build will continue even if there are compilation
  errors; defaults to true.
  Deprecated. No reason given

fork (Default: false)
  Allows running the compiler in a separate process. If 'false' it uses the
  built in compiler, while if 'true' it will use an executable.
  Deprecated. No reason given

includes
  A list of inclusion filters for the compiler.
  Deprecated. No reason given

maxmem
  Sets the maximum size, in megabytes, of the memory allocation pool, ex.
  '128', '128m' if fork is set to true.
  Deprecated. No reason given

meminitial
  Initial size, in megabytes, of the memory allocation pool, ex. '64',
  '64m' if fork is set to true.
  Deprecated. No reason given

optimize (Default: false)
  Set to true to optimize the compiled code using the compiler's
  optimization methods.
  Deprecated. No reason given

outputFileName
  Sets the name of the output file when compiling a set of sources to a
  single file.
  Deprecated. No reason given

showDeprecation (Default: false)
  Sets whether to show source locations where deprecated APIs are used.
  Deprecated. No reason given

showWarnings (Default: false)
  Set to true to show compilation warnings.
  Deprecated. No reason given

source
  The -source argument for the Java compiler.
  Deprecated. No reason given

staleMillis (Default: 0)
  Sets the granularity in milliseconds of the last modification date for
  testing whether a source needs recompilation.
  Deprecated. No reason given

target
  The -target argument for the Java compiler.
  Deprecated. No reason given

verbose (Default: false)
  Set to true to show messages about what the compiler is doing.
  Deprecated. No reason given

That’s more like it. If you ever find yourself wondering what a particular plugin configuration parameter’s name is, or what a default value for a parameter is. You now know how to find it.