How to read an external properties file in Maven Ask Question. Asked 12 years, 8 months ago. Active 8 months ago. Viewed k times.
Hamedz 15 15 silver badges 25 25 bronze badges. Dougnukem Dougnukem If you have only a few values, and different users will need different values, consider instead putting the value in your settings. Add a comment. Active Oldest Votes. Mike Pone Mike Pone I think that's what I'm looking for I couldn't find the 1. Current link: mojo. I had problems with this plugin on Windows. If someone has problems too, try out kuali instead. Show 2 more comments. The Alchemist 3, 19 19 silver badges 22 22 bronze badges.
Thanks for a working example. Below code excludes any file name ending with. This code also helpful to exlude anyother file types. All you need to do is provide proper file extension. Below code excludes any file ends with. I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment. It is also possible to add new goals to the process, and configure specific goals.
For information on this, see the Introduction to the Build Lifecycle. To find out what configuration is available for a plugin, you can see the Plugins List and navigate to the plugin and goal you are using. For general information about how to configure the available parameters of a plugin, have a look at the Guide to Configuring Plugins. Another common use case that can be satisfied which requires no changes to the POM that we have above is packaging resources in the JAR file.
For this common task, Maven again relies on the Standard Directory Layout , which means by using standard Maven conventions you can package resources within JARs simply by placing those resources in a standard directory structure. If you unpacked the JAR that Maven created for you and took a look at it you would see the following:.
MF as well as a pom. These come standard with generation of a JAR in Maven. You can create your own manifest if you choose, but Maven will generate one by default if you don't. You can also modify the entries in the default manifest.
We will touch on this later. The pom. One simple use might be to retrieve the version of your application. Operating on the POM file would require you to use some Maven utilities but the properties can be utilized using the standard Java API and look like the following:. At this point you would have a project directory structure that would look like the following:.
In a unit test you could use a simple snippet of code like the following to access the resource required for testing:. Sometimes a resource file will need to contain a value that can only be supplied at build time. The property can be one of the values defined in your pom. To have Maven filter resources when copying, simply set filtering to true for the resource directory in your pom. You'll notice that we had to add the build , resources , and resource elements which weren't there before. All of this information was provided as default values previously, but because the default value for filtering is false, we had to add this to our pom.
To reference a property defined in your pom. Note that some elements of the POM have default values, so don't need to be explicitly defined in your pom. Similarly, values in the user's settings. To continue our example, let's add a couple of properties to the application. With that in place, you can execute the following command process-resources is the build lifecycle phase where the resources are copied and filtered :.
To reference a property defined in an external file, all you need to do is add a reference to this external file in your pom.
As an alternative to defining the my. Filtering resources can also get values from system properties; either the system properties built into Java like java. To continue the example, let's change our application. Now, when you execute the following command note the definition of the command. You've probably already noticed a dependencies element in the POM we've been using as an example. You have, in fact, been using an external dependency all this time, but here we'll talk about how this works in a bit more detail.
For a more thorough introduction, please refer to our Introduction to Dependency Mechanism. The dependencies section of the pom. Right now, our project is depending on JUnit only I took out all of the resource filtering stuff for clarity :. For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope. The groupId, artifactId, and version are the same as those given in the pom. The scope element indicates how your project uses that dependency, and can be values like compile , test , and runtime.
For more information on everything you can specify for a dependency, see the Project Descriptor Reference. For more information about the dependency mechanism as a whole, see Introduction to Dependency Mechanism. With this information about a dependency, Maven will be able to reference the dependency when it builds the project.
Where does Maven reference the dependency from? In a previous section , we installed the artifact from our project my-app Once it's installed there, another project can reference that jar as a dependency simply by adding the dependency information to its pom.
What about dependencies built somewhere else? How do they get into my local repository? Whenever a project references a dependency that isn't available in the local repository, Maven will download the dependency from a remote repository into the local repository. You probably noticed Maven downloading a lot of things when you built your very first project these downloads were dependencies for the various plugins used to build the project. You can also set up your own remote repository maybe a central repository for your company to use instead of or in addition to the default remote repository.
For more information on repositories you can refer to the Introduction to Repositories. Let's add another dependency to our project. Let's say we've added some logging to the code and need to add log4j as a dependency. First, we need to know what the groupId, artifactId, and version are for log4j. In that directory is a file called maven-metadata.
Here's what the maven-metadata.
0コメント