# Copyright (c) 2012-2016, Red Hat, Inc
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of Red Hat nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Stanislav Ochotnicky <sochotnicky@redhat.com>
#          Mikolaj Izdebski <mizdebsk@redhat.com>


# %mvn_package - assign Maven artifact(s) to a subpackage
#
# Usage: %mvn_package [gId]:[aId[:ver]] <subpackage name>
#
# This macro causes given artifact(s) to be installed in given subpackage.
#
# GId, aId and ver can contain wildcards and alternatives, which allows
# multiple artifacts to be matched. If any of gId, aId or ver is omited
# then any value will match the respective part. In particular a bare :
# symbol will match any artifact.
#
# Subpackage name can contain backreferences in form @n.
#
%mvn_package /usr/bin/python3 /usr/share/java-utils/mvn_package.py


# %mvn_alias - create alias(es) for given Maven artifact(s)
#
# Usage: %mvn_alias [gId]:[aId[:ver]] [aliasGId]:[aliasAId[:aliasVer]] [...]
#
# This macro adds one or more aliases for specified artifacts. Aliases are
# additional groupId, artifactId and version triples under which artifacts
# can be found. After resulting package is installed any attempts to
# resolve any of aliases will result in resolving the original artifact.
#
# GId, aId and ver can contain wildcards and alternatives, which allows
# multiple artifacts to be matched. If any of gId, aId or ver is omited
# then any value will match the respective part. In particular a bare :
# symbol will match any artifact.
#
# AliasGId, aliasAId and aliasVer can contain backreferences in form @n,
# which can refer only to respective parts.
#
%mvn_alias /usr/bin/python3 /usr/share/java-utils/mvn_alias.py


# %mvn_file - specify file name(s) for Maven artifact(s)
#
# Usage: %mvn_file [gId]:[aId[:ver]] <file1> [file2 ...]
#
# GId, aId and ver can contain wildcards and alternatives, which allows
# multiple artifacts to be matched. If any of gId, aId or ver is omited
# then any value will match the respective part. In particular a bare :
# symbol will match any artifact.
#
# File names can contain backreferences in form @n. If multiple names
# are given then the first one will be primary name of the artifact and
# additional names will be symbolic links pointing to the primary file.
#
%mvn_file /usr/bin/python3 /usr/share/java-utils/mvn_file.py

# %mvn_compat_version - specify file name(s) for Maven artifact(s)
#
# Usage: %mvn_compat_version [gId]:[aId[:ver]] <version1> [version2 ...]
#
# GId, aId and ver can contain wildcards and alternatives, which allows
# multiple artifacts to be matched. If any of gId, aId or ver is omited
# then any value will match the respective part. In particular a bare :
# symbol will match any artifact.
#
# Versions can contain backreferences in form @n. If multiple versions are
# given then the first one will be primary version of the artifact and
# additional versions will be symbolic links pointing to the primary
# version.
#
%mvn_compat_version /usr/bin/python3 /usr/share/java-utils/mvn_compat_version.py


# %mvn_config - set XMvn configuration option
#
# Usage: %mvn_config <option> <value>
#
# This macro sets value of specified XMvn configuration option.
#
# Option is a sequence of slash-delimited XML node names that identify
# XMvn configuration option, as in XMvn configuration reference; for
# example: buildSettings/compilerSource.
#
# For information about available configuration options, see XMvn
# configuration reference, available on XMvn website.
#
%mvn_config /usr/bin/python3 /usr/share/java-utils/mvn_config.py


# %mvn_artifact - add Maven artifact to be installed
#
# Usage: %mvn_artifact <pom> [<artifact-file>]
#
%mvn_artifact /usr/bin/python3 /usr/share/java-utils/mvn_artifact.py


# %mvn_build - build Maven project
#
# Usage: %mvn_build [options]
#
# This macro causes Maven project to be built (usually compiled and
# packaged). It is intended to be placed in %build section of spec file.
#
# For summary of accepted options execute `mvn-build --help` command.
#
%mvn_build %{?jpb_env} /usr/bin/python3 /usr/share/java-utils/mvn_build.py --xmvn-javadoc %{?base_xmvn_opts} %{?xmvn_opts} %{?xmvn_bootstrap: -b} %{?_without_javadoc: -j $(> .mfiles-javadoc)}%{?_without_tests: -f}


# %gradle_build - build Gradle project
#
# Usage: %mvn_build [options]
#
# This macro causes Gradle project to be built (usually compiled and
# packaged). It is intended to be placed in %build section of spec file.
#
# For summary of accepted options execute `mvn-build --help` command.
#
# %gradle_build %{?jpb_env} /usr/bin/python3 /usr/share/java-utils/mvn_build.py --gradle %{?base_xmvn_opts} %{?xmvn_opts} %{?xmvn_bootstrap: -b} %{?_without_javadoc: -j $(> .mfiles-javadoc)}%{?_without_tests: -f}


# %mvn_install - install Maven project
#
# Usage: %mvn_install
#
# This macro causes previously built Maven project to be installed into
# buildroot. It is intended to be placed in %install section of spec file.
#
%mvn_install(J:X) %{?jpb_env} xmvn-install %{?base_xmvn_opts} %{?xmvn_install_opts} %{?-X} -R .xmvn-reactor -n %{name} %{?xmvn_install_repo:-i %{xmvn_install_repo}} -d "%{buildroot}" \
%{-J*:jdir="%{-J*}"}%{!-J*:jdir=target/site/apidocs; [ -d .xmvn/apidocs ] && jdir=.xmvn/apidocs} \
%{__mkdir_p} %{buildroot}%{_licensedir} \
if [ -d "${jdir}" ]; then \
   install -dm755 %{buildroot}%{_javadocdir}/%{name} \
   cp -pr "${jdir}"/* %{buildroot}%{_javadocdir}/%{name} \
   echo '%{_javadocdir}/%{name}' >>.mfiles-javadoc \
fi \
%{nil}


# %mvn_subst - replace all JAR files in given directory with symlinks
#
# Usage: %mvn_subst [options] path
#
# Substitutes Java archives in given directory with symlinks to system-wide or
# just installed versions.
%mvn_subst %{?jpb_env} xmvn-subst %{?base_xmvn_opts} %{?xmvn_subst_opts} -R %{buildroot}


#==============================================================================
#
# %pom_* -- macros for batch editing of POM files
#
# These macros can be used to edit Maven POM files directly from RPM spec
# file. Using these macros it is possible to alter Maven dependency management,
# change plugin invocation, or even inject or remove arbitrary XML code to/from
# nodes described by an XPath expression.
#
# See comments preceding individual macros for more detailed documentation.
#

# Private macro, shouldn't be used directly
%__pom_call           /usr/bin/python3 /usr/share/java-utils/pom_editor.py pom_


# %pom_remove_dep - remove dependency on Maven artifact from POM file
#
# Usage: %pom_remove_dep [groupId]:[artifactId] [POM location]
#
# This macro patches specified POM file not to contain dependencies on given
# Maven artifact.
#
# groupId and artifactId are identifiers of Maven group and artifact of the
# module on which dependency is to be removed. If they are ommited then all
# identifiers are matched.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_remove_dep     %{__pom_call}remove_dep 


# %pom_remove_plugin - remove Maven plugin invocation from POM file
#
# Usage: %pom_remove_plugin [groupId]:[artifactId] [POM location]
#
# This macro patches specified POM file not to contain invocations of given
# Maven plugin.
#
# groupId and artifactId are identifiers of Maven group and artifact of the
# plugin which invocation is to be removed. If they are ommited then all
# identifiers are matched.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_remove_plugin  %{__pom_call}remove_plugin 


# %pom_disable_module - disable given project module in POM file
#
# Usage: %pom_disable_module <module name> [POM location]
#
# This macro patches specified POM file not to contain reference to given
# project module.
#
# Module name is the exact name of the module to be disabled. It must not be
# ommited.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_disable_module %{__pom_call}disable_module 


# %pom_xpath_remove - remove an XML node from POM file
#
# Usage: %pom_xpath_remove <XPath> [POM location]
#
# This macro patches specified POM file removing all XML nodes described by the
# XPath expression.
#
# XPath is an expression describing a set of XML nodes to be removed from the
# POM file. It must be a properly formated XPath 1.0 expression, as described
# in <http://www.w3.org/TR/xpath/>.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
# NOTE: POM files use a specific namespace - http://maven.apache.org/POM/4.0.0.
# The easiest way to respect this namespace in XPath expressions is prefixing
# all node names with "pom:". For example, "pom:environment/pom:os" will work
# because it selects nodes from pom namespace, but "environment/os" won't find
# anything because it looks for nodes that don't belong to any XML namespace.
#
%pom_xpath_remove   %{__pom_call}xpath_remove 

%pom_xpath_disable   %{__pom_call}xpath_disable 

# %pom_xpath_inject - inject XML code into POM file
#
# Usage: %pom_xpath_inject <XPath> [XML code] [POM location]
#
# This macro patches specified POM file appending some code as children of all
# XML nodes described by the XPath expression.
#
# XPath is an expression describing a set of XML nodes in the POM file to which
# child code is to be appended. It must be a properly formated XPath 1.0
# expression, as described in <http://www.w3.org/TR/xpath/>.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_xpath_inject   %{__pom_call}xpath_inject 


# %pom_xpath_replace - replace XML node from POM file with given XML code
#
# Usage: %pom_xpath_replace <XPath> <XML code> [POM location]
#
# This macro patches specified POM file removing all XML nodes described by the
# XPath expression and injecting given XML code in their place.
#
# XPath is an expression describing a set of XML nodes to be removed from the
# POM file. It must be a properly formated XPath 1.0 expression, as described
# in <http://www.w3.org/TR/xpath/>.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_xpath_replace  %{__pom_call}xpath_replace  


# %pom_xpath_set - set text contents of XML node from POM file
#
# Usage: %pom_xpath_set <XPath> <new contents> [POM location]
#
# This macro patches specified POM file replacing text contents of all XML nodes
# described by the XPath expression with given new text content.
#
# XPath is an expression describing a set of XML nodes to be removed from the
# POM file. It must be a properly formated XPath 1.0 expression, as described
# in <http://www.w3.org/TR/xpath/>.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_xpath_set      %{__pom_call}xpath_set  


# %pom_add_parent - add parent POM reference to POM file
#
# Usage: %pom_add_parent groupId:artifactId[:version] [POM location]
#
# This macro patches specified POM file adding a reference to parent POM with
# specified groupId, artifactId and version.
#
# groupId and artifactId are identifiers of Maven group and artifact of the POM
# to be referenced as parent POM. They must be specified. If version is not
# specified then default value of "any" is used.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_add_parent     %{__pom_call}add_parent    


# %pom_remove_parent - remove parent POM reference from POM file
#
# Usage: %pom_remove_parent [POM location]
#
# This macro patches specified POM file removing reference to parent POM.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
%pom_remove_parent  %{__pom_call}remove_parent  


# %pom_set_parent - set parent POM reference in POM file
#
# Usage: %pom_set_parent groupId:artifactId[:version] [POM location]
#
# This macro patches specified POM file setting reference to parent POM to
# specified groupId, artifactId and version.
#
# This macros first removes parent POM reference and then adds a new one. It's
# (more or less) equivalent to call to %pom_remove_parent followed by
# invocation of %pom_add_parent. See documentation for these macros for more
# information about parameters.
#
%pom_set_parent     %{__pom_call}set_parent  


# %pom_add_dep[_mgmt] - add dependency to POM file
#
# Usage: %pom_add_dep groupId:artifactId[:version[:scope]] [POM location] \
#            [extra XML]
#        %pom_add_dep_mgmt groupId:artifactId[:version[:scope]] [POM location] \
#            [extra XML]
#
# These macros patch specified POM file adding a dependency on given version of
# artifactId from groupId, with given scope (if any). %pom_add_dep adds the
# dependency to <dependencies> XML node, while %pom_add_dep_mgmt adds it to
# <dependencyManagement>.
#
# groupId and artifactId are identifiers of Maven artifact on which dependency
# is to be added. They must be specified. If version is not specified then
# default value of "any" is used. If scope is not specified then no scope is
# defined (Maven defaults to "compile" in this case).
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
# Extra XML is additional XML code to be added into the <dependency> node. This
# parameter can be ommited, in which case no extra XML is appended.
#
%pom_add_dep        %{__pom_call}add_dep  
%pom_add_dep_mgmt   %{__pom_call}add_dep_mgmt 


# %pom_add_plugin - add plugin invocation to POM file
#
# Usage: %pom_add_plugin groupId:artifactId[:version] [POM location] \
#            [extra XML]
#
# This macro patches specified POM file adding plugin invocation of given
# groupId and artifactId.
#
# groupId and artifactId are identifiers of Maven plugin artifact of which
# invocation is to be added. groupId may be ommited, in which case value of
# "org.apache.maven.plugins" is used as default. artifactId must be
# specified. If version is not specified then default value of "any" is used.
#
# POM location can be either a full path to the POM file, or a path to the
# directory containing pom.xml. If POM location is not given then pom.xml from
# current working directory is used.
#
# Extra XML is additional XML code to be added into the <plugin> node. This
# parameter can be ommited, in which case no extra XML is appended.
#
%pom_add_plugin     %{__pom_call}add_plugin 

%pom_change_dep     %{__pom_call}change_dep

# %javadoc_package - generate javadoc subpackage definition
#
# Usage: %javadoc_package [options]

# This macro generates %package and %files definitions for a javadoc subpackage.
# Use on top-level only, preferably just before %prep.
# Prerequisites:
# - javadoc package must not already exist
# - package must use %mvn_install to install javadoc (default when using %mvn_build)
# - package should install license files using %license macro, or specify
#   license for javadoc package manually (see below)
#
# Manually specifying license files:
# Use -L option to suppress automatic license installation and specify %license
# directive on the next line after the macro invocation. Example:
# %javadoc_package -L
# %license LICENSE
%javadoc_package(L) %if 0%{!?_without_javadoc:1} \
%package javadoc \
BuildArch: noarch \
Summary: API documentation for %{name} \
Group: Development/Documentation \
%description javadoc \
API documentation for %{name}. \
%files javadoc -f .mfiles-javadoc \
%endif
