001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.enforcer.rules; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023 024import java.util.ArrayList; 025import java.util.List; 026import java.util.Map; 027import java.util.Objects; 028 029import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 030import org.apache.maven.project.MavenProject; 031import org.codehaus.plexus.util.StringUtils; 032 033/** 034 * This rule checks that some profiles are active. 035 * 036 * @author <a href="mailto:brianf@apache.org">Brian Fox</a> 037 */ 038@Named("requireActiveProfile") 039public final class RequireActiveProfile extends AbstractStandardEnforcerRule { 040 041 /** Comma separated list of profiles to check. 042 */ 043 private String profiles = null; 044 045 /** If all profiles must be active. If false, only one must be active 046 */ 047 private boolean all = true; 048 049 private final MavenProject project; 050 051 @Inject 052 public RequireActiveProfile(MavenProject project) { 053 this.project = Objects.requireNonNull(project); 054 } 055 056 public String getProfiles() { 057 return profiles; 058 } 059 060 public void setProfiles(String profiles) { 061 this.profiles = profiles; 062 } 063 064 public boolean isAll() { 065 return all; 066 } 067 068 public void setAll(boolean all) { 069 this.all = all; 070 } 071 072 @Override 073 public void execute() throws EnforcerRuleException { 074 List<String> missingProfiles = new ArrayList<>(); 075 if (StringUtils.isNotEmpty(profiles)) { 076 String[] profileIds = profiles.split(","); 077 for (String profileId : profileIds) { 078 if (!isProfileActive(project, profileId)) { 079 missingProfiles.add(profileId); 080 } 081 } 082 083 boolean fail = false; 084 if (!missingProfiles.isEmpty()) { 085 if (all || missingProfiles.size() == profileIds.length) { 086 fail = true; 087 } 088 } 089 090 if (fail) { 091 String message = getMessage(); 092 StringBuilder buf = new StringBuilder(); 093 if (message != null) { 094 buf.append(message + System.lineSeparator()); 095 } 096 097 for (String profile : missingProfiles) { 098 buf.append("Profile \"" + profile + "\" is not activated." + System.lineSeparator()); 099 } 100 101 throw new EnforcerRuleException(buf.toString()); 102 } 103 } 104 } 105 106 /** 107 * Checks if profile is active. 108 * 109 * @param project the project 110 * @param profileId the profile name 111 * @return <code>true</code> if profile is active, otherwise <code>false</code> 112 */ 113 private boolean isProfileActive(MavenProject project, String profileId) { 114 for (Map.Entry<String, List<String>> entry : 115 project.getInjectedProfileIds().entrySet()) { 116 if (entry.getValue().contains(profileId)) { 117 return true; 118 } 119 } 120 return false; 121 } 122 123 @Override 124 public String toString() { 125 return String.format("RequireActiveProfile[message=%s, profiles=%s, all=%b]", getMessage(), profiles, all); 126 } 127}