apache/maven · error · IllegalArgumentException

contains function requires exactly two arguments

Error message

contains function requires exactly two arguments

What it means

contains() is a built-in function of Maven 4.1 profile activation conditions (<activation><condition>). ConditionFunctions.contains throws IllegalArgumentException unless exactly two string arguments (string, substring) are supplied; the activator reports 'Error parsing profile activation condition' and leaves the profile inactive.

Source

Thrown at impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionFunctions.java:138

    public Object indexOf(List<Object> args) {
        if (args.size() != 2) {
            throw new IllegalArgumentException("indexOf function requires exactly two arguments");
        }
        String s = ConditionParser.toString(args.get(0));
        String substring = ConditionParser.toString(args.get(1));
        return s.indexOf(substring);
    }

    /**
     * Checks if a string contains a given substring.
     *
     * @param args A list containing two strings: the main string and the substring to check
     * @return true if the main string contains the substring, false otherwise
     * @throws IllegalArgumentException if the number of arguments is not exactly two
     */
    public Object contains(List<Object> args) {
        if (args.size() != 2) {
            throw new IllegalArgumentException("contains function requires exactly two arguments");
        }
        String s = ConditionParser.toString(args.get(0));
        String substring = ConditionParser.toString(args.get(1));
        return s.contains(substring);
    }

    /**
     * Checks if a string matches a given regular expression.
     *
     * @param args A list containing two strings: the string to check and the regex pattern
     * @return true if the string matches the regex, false otherwise
     * @throws IllegalArgumentException if the number of arguments is not exactly two
     */
    public Object matches(List<Object> args) {
        if (args.size() != 2) {
            throw new IllegalArgumentException("matches function requires exactly two arguments");
        }
        String s = ConditionParser.toString(args.get(0));

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Use exactly two arguments in the order (string, substring): contains(${os.name}, 'Linux')
  2. For case-insensitive matching wrap both sides: contains(lower(${os.name}), 'linux')
  3. Verify with mvn help:active-profiles after the fix

Example fix

<!-- before -->
<condition>contains(${os.name})</condition>

<!-- after -->
<condition>contains(${os.name}, 'Linux')</condition>
Defensive patterns

Strategy: validation

Validate before calling

static boolean arityOk(String condition, String fn, int min, int max) {
    int i = condition.indexOf(fn + "(");
    if (i < 0) return true;
    String args = condition.substring(i + fn.length() + 1, condition.indexOf(')', i));
    int n = args.isBlank() ? 0 : args.split(",", -1).length;
    return n >= min && n <= max;
}
boolean ok = arityOk(condition, "contains", 2, 2);

Try / catch

try {
    Object r = new ConditionParser(functions, resolver).parse(condition);
} catch (IllegalArgumentException e) {
    // report and treat the profile as inactive
}

Prevention

When it happens

Trigger: A condition like contains(${os.name}) with only one argument or contains('Linux', ${os.name}, true) with three — the only valid form is contains(haystack, needle).

Common situations: Forgetting the needle while sketching a condition; adding a case-sensitivity flag that does not exist (combine with lower()/upper() instead); argument order swapped, which yields wrong results rather than an error.

Related errors


AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21). Data as JSON: /api/errors/e5e36b717a8b6555. Report an issue: GitHub.