currently we are using ANT script to build the project and running jUnit tests. Now, we decided to move to Maven.
We have two web projects, Core-Project and Sub-Project. Now here it gets complicate. Their project is as follow.
Now, If I want to add this Sub-Project to Core-Project then I will create a jar of Sub-Project with WebContent folder and put it into Core-Project. Now whenever I run Core-Project, we have one utility class which extract content of Sub-Project into Core-Project.
Final(expected) project structure should look like this.
How can I achieve this in Maven? I mean how do I create a jar which contain some files located in src->main->webapp.
It seems that you need to rethink your architecture a bit. This answer may go beyond the scope of your question, but it's important to treat Maven as 'convention over configuration'. It is possible to achieve your layout using a combination of maven packaging tools, but if you restructure and follow Maven conventions, it will make more sense to people outside your project and be less work to maintain.
Suggested:
config-project
sub-project
core-project
config-project can hold the configuration for all parts of your application. sub-project and core-project can depend on this project and use it at runtime. You should package this project as 'jar' or 'zip' depending on the resources you need to make available to other projects.
sub-project should only contain the binary code common to the non-web based part of your application. It should be packaged as a 'jar' project and not be packaged with the config-project dependencies.
core-project should be packaged as a 'war' project. and follow the directory structure as suggested here: Maven War Plugin
Keeping the separation between your configuration, your non-web code and your web code will take a little bit to get used to. There is an excellent archetype by Tomcat which generates a maven project structure composed of these parts. It is easy to generate and inspect: Maven Tomcat Archetype
Related
I have just started working with Maven in Eclipse.
I tried running a sample program in it, and I encountered some problems. The errors were in my pom.xml file. What is the significance of the pom.xml file in Maven?
In short the pom.xml will have all information to build your project.
For example you want to build a project and you have only the pom.xml sent to you via mail. If there are enough entries in the pom.xml then that is all you need! You can import it to Eclipse, Maven will download your source code from CVS, download various dependency jars (like Spring, Apache Commons), run your test cases, build the jar/war, deploy to your jboss/app server, generate a report of your code quality (using Sonar, maybe). Each task you want to do will be mentioned as a goal.
The links already provided are good enough for reference.
POM is an XML file that contains the project configuration details used by Maven. It provides all the configurations required for a project.
POM means Project Object Model, and, as the name suggests, it defines the model of the project as well.
In the normal project development you will add JAR files and libraries as required. In Maven-based development, those JAR files, libraries are added to the project using this pom.xml. In the pom context we call those JAR files, libraries as dependencies.
Maven is a build tool and pom.xml is the main file for the project.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of the information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
For more reference, check Maven in 5 Minutes.
POM stands for project object model. It's the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used to build the project. It downloads required libraries easily using POM XML tags.
When there is no Maven, it needs to add all the library JAR files one by one to the project. But when there is Pom.xml there is no need to add library JAR files one by one.
Simply add the dependency to the Pom.xml, and it will automatically add the library JAR files to the project.
pom.xml is a file which describes the project, configures plugins, and declares dependencies. The POM names the project, provides a set of unique identifiers (called coordinates) for a project, and defines the relationships between this project and others through dependencies, parents, and prerequisites.
A POM file can include a modules section, which tells Maven which directories have POM files which need to be built.
In the build section you can define plugins for which you need to build the artifacts in your project.
Pom.xml is part of your maven project, using pom.xml, maven life cycle you can achieve it.
The pom.xml is a project object model which tells everything 3rd party tool dependencies and library's and required plugins it will give everything to your project like project means any java based web content like itself. once you create the maven project you will get the pom.xml blog which is everything to handle your project
If you want to test your project, you need to add testing dependencies which is the maven community it will provide you once added it will have everything tested.
I am developing a Java application using IntelliJ Idea 14.1.4.
If it would have been solely Java application, I would have known exactly how to structure the project in Idea:
A single Java project, containing several modules: One for each part of the application (JAR).There will be at most 4-5 JARs.
The dependencies between the modules are also known: Protocol does not depend on anything, everything else depends on Infrastructure, and so on.
Next, I would like to use Gradle scripting for managing the project. So my question is what is the best practice to structure the code in Idea?
Should I create a single Gradle Project and a Gradle module for each of the JARs?
Should I create a single Java project (or maybe empty project) and Gradle modules for each of the JARs?
Should I create a single Gradle project and each of the modules will be a Gradle's sub-project? Maybe it will be better to have an empty project and several Gradle modules because not all of the JARs are closely coupled?
Since I have never used Gradle before, I would appreciate any guidance for the best practices when combining both Gradle and Idea.
Thanks,
Guy
As long as possible, I would keep the code in one source repository. On the root, I would have an "empty" project not outputting anything. All your jar projects would be sub-projects (in Gradle terms). You include them via the settings.xml file located in the root project.
Each sub-project has its own build.gradle file. In those files, you can easily define the dependencies between your sub-projects, e.g.:
dependencies {
compile project(':subProject3')
}
For convenience, I often create a special export task to put all artefacts in one export/ folder on the root level so that you don't need to go through all those sub-folders to get your stuff.
task export(type: Copy) {
from project(':subProject1').jar
from project(':subProject2').jar
from project(':subProject3').war
into 'export/'
}
IntelliJ Ultimate 14 works fine with this approach. You can simply hit Make to compile everything. You might also want to configure your project settings to run gradle jar or gradle export during a make if you prefer.
I have an Eclipse project that depends on several other projects from the same workspace. I know hot to create a simple Ant build script for a single independent Java project, but how to tell Ant to integrate all those project into my main JAR file so my dependent program can work?
In Ant, the simplest way is to explicitly enumerate those other projects and include them in the jar target. Something like:
<path id="all-projects">
<fileset dir="../my.other.project" includes="**/*class">
</path>
One solution : Create a master project and put all the projects into it and have a single build.xml.
Another option : Create a master project but leave everything at the same level (do not place project-components inside the project folder) Then arrange your build script to refer to the other projects via relative paths. Note that with some build tools this is not even necessary as they will look at their repository.
While you can munge together a solution with Ant, Apache Maven is a better solution for managing dependencies than Ant. If you don't want to go with a full blown Maven lifecycle management project, you can also look at using a Nexus repository to store your artifacts, and include Apache Ivy in your Ant build scripts to perform dependency management.
Ivy or Maven will provide a better, more manageable dependency management strategy which is likely to be easier to integrate into a team than anything you will be able to assemble on your own.
What is the best way to add project in Maven Central repository if it uses jar-file and web-resources (js, css, images)?
I can't find any good way to do it so users could add dependency in pom.xml and get all required files (not only jar-file).
I found WebJars project, but it looks a little compicated and requires additional steps (adding and configuring webjars maven plugin before getting my project).
It would be useful to have another project with similar structure.
Thanks for your help!
OK - if you want to distrubute a webapp as a complete package the most common thing to do is package your application as a WAR.
To do this you just need to add the following to your pom:
<packaging>war</packaging>
Don't forget to ensure that your project conforms to the layout specified by the Maven WAR Plugin.
Once you have your WAR deployed, other WAR projects can depend on it which produce a single WAR that is a combination of the two.
If you need something a little more complicated look at using the Maven Assembly Plugin that allows you to create custom archives. As with the WAR, other assembly projects can depend on it, though as the unpacking is in your hands there is again an increase level of complexity.
I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.